我第一次使用htaccess为我的网站html文件和1个php文件制作漂亮的网址。我只是想知道我能否就我的htaccess文件设置得到一些建议,如果我设置它是一个好方法?因为我所写的内容,我讨厌我的网址在某些情况下无法工作。 :(
示例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]
答案 0 :(得分:1)
这一行
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L]
会发送一些页面到phppage.php,即使它们看起来不像
http://www.domain.com/subdomain/phppage/1/
因为在RewriteRule的第一个参数中没有提到phppage。
答案 1 :(得分:1)
尝试将以下内容添加到域根目录中的.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]