【发布时间】:2014-01-25 14:00:50
【问题描述】:
我的 .htaccess 文件有问题。好吧,我正在尝试做两件事;删除所有 URL 的扩展名“.php”,例如:“localhost/about.php”到“localhost/about”或“localhost/about/”。并重写一个动态url:“localhost/user/index.php?usr=username”为“localhost/user/username/”或“localhost/username”。
我找到了一种方法来做这两件事。但是,如果我有删除“.php”扩展名的代码,那么重写动态 url 的代码就不起作用了。如果我没有删除“.php”扩展名的代码,那么重写动态 url 的代码就可以了。
这是我尝试打开个人资料页面时网站给我的错误:
找不到
在此服务器上找不到请求的 URL /user/enric。
或
找不到
在此服务器上找不到请求的 URL /user/enric.php。
我能做什么?这是.htaccess文件的代码:
<IfModule mod_rewrite.c>
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]
# Delete ".php" extension and adds "/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
# User profiles
RewriteRule ^user/([^/]*)/$ /user/index/?usr=$1 [L,R=301]
</IfModule>
解决方案,作者 JON LIN
<IfModule mod_rewrite.c>
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L]
# User profiles
RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1 [L]
# Delete ".php" extension and adds "/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
</IfModule>
【问题讨论】:
标签: php .htaccess mod-rewrite dynamic url-rewriting