【发布时间】:2015-01-03 00:45:23
【问题描述】:
我有我的 htaccess 文件设置,以便页面删除扩展名。现在,我正在尝试将传输变量的页面转换为对 SEO 友好的 url...所以,例如...
http://www.example.com/art-gallery?page=2 ...实际上是“art-gallery.php?page=2”,会变成...http://www.example.com/art-gallery/page/2
或者……http://www.example.com/art-piece?id=3……会去……http://www.example.com/art-piece/id/3
...等等...
我的 htaccess 文件中有很多内容,但不确定如何执行上述操作(有很多关于从 www.example.com/index.php?page=2 到 www.example.com/page 的教程/2/ 但没有一个能完全满足我的需要)。理想情况下,我希望能够对所有类似页面执行此操作...
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# Force www:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
# Remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# Remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# Remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# Forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Disable Directory Browsing
Options -Indexes
# Disable Hotlinking of Images
# with forbidden or custom image option
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
# Protect htaccess File
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
【问题讨论】:
标签: .htaccess mod-rewrite