Htaccess красивые настройки URL - PullRequest
       1

Htaccess красивые настройки URL

1 голос
/ 01 декабря 2011

Я впервые использую htaccess, чтобы создать красивые URL для html-файлов моего сайта и 1 php-файла.Мне просто интересно, смогу ли я получить некоторые советы по настройке моего файла htaccess, и если как я его настрою, это хороший способ?Я бы не хотел, чтобы мои URL не работали в некоторых ситуациях из-за того, что я написал.: (

Пример html-файла:

before:  http://www.domain.com/subdomain/htmlpage.html
after:   http://www.domain.com/subdomain/htmlpage/

Отдельный файл php:

before:  http://www.domain.com/subdomain/phppage.php?p=1
after:   http://www.domain.com/subdomain/phppage/1/

Я добавил в правило перенаправления index.html в index.phpМне также пришлось добавить 'base href' в заголовок каждого файла, потому что я использовал относительные ссылки.

файл htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L]

Ответы [ 2 ]

1 голос
/ 01 декабря 2011

попробуйте добавить следующее в ваш файл .htaccess в корне вашего домена

Options +FollowSymLinks

RewriteEngine On
RewriteBase /

# redirect http://www.domain.com/subdomain/htmlpage.html to http://www.domain.com/subdomain/htmlpage/
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.html$ [NC]
RewriteRule . %1/ [L,R=301]


#redirect http://www.domain.com/subdomain/phppage.php?p=1 to http://www.domain.com/subdomain/phppage/1/
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.php$ [NC]
# or alternatively if page is literally phppage uncomment below and comment above
#RewriteCond %{REQUEST_URI} ^(/subdomain/phppage)\.php$ [NC]
RewriteRule . %1/ [L,R=301]

#if url does not end with /
RewriteCond %{REQUEST_URI} ^(/subdomain/.+[^/])$ [NC]
# and its not for an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# put one trailing slash
RewriteRule . %1/ [L,R=301]

#write http://www.domain.com/subdomain/htmlpage/ to htmlpage.html
RewriteCond %{REQUEST_URI} ^/subdomain/([^/]+)/$ [NC]
RewriteRule . %1.html [L]   


#write http://www.domain.com/subdomain/phppage/1/ to phppage.php?p=1
RewriteCond %{REQUEST_URI} ^/subdomain/[^/]+/([0-9]+)/$ [NC]
RewriteRule . phppage.php?p=%1 [L]  
1 голос
/ 01 декабря 2011

Эта строка

RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L]

собирается отправить некоторые страницы в phppage.php, даже если они не выглядят как

http://www.domain.com/subdomain/phppage/1/

потому что в первом аргументе RewriteRule нет упоминания о phppage.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...