我的.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 / 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>
答案 0 :(得分:0)
您需要添加一个检查,当您向请求添加.php
扩展名时,php文件实际存在,并且您应该移动您的用户在您的php扩展程序之前重写 :
<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>
答案 1 :(得分:0)
您可以通过在线工具生成htaccess规则 http://www.generateit.net/mod-rewrite/index.php