Php .htaccess重定向带有参数的简单友好url
我试图使我的网站的URL更具可读性,但我在这方面遇到了困难 她的是URL的示例,它现在看起来像:Php .htaccess重定向带有参数的简单友好url,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我试图使我的网站的URL更具可读性,但我在这方面遇到了困难 她的是URL的示例,它现在看起来像: http://example.com/ http://example.com/index.php http://example.com/index.php?page=home http://example.com/index.php?page=profile&uid=483ec3a0-7e0f-32e6-f357-ac9311204246 它们必须看起来像: http://example.
http://example.com/
http://example.com/index.php
http://example.com/index.php?page=home
http://example.com/index.php?page=profile&uid=483ec3a0-7e0f-32e6-f357-ac9311204246
它们必须看起来像:
http://example.com/
http://example.com/home/
http://example.com/profile/483ec3a0-7e0f-32e6-f357-ac9311204246/
以下是my.htaccess文件的内容:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+)&uid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&uid=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
它重写了一个正确的URL,如http://example.com/index.php?page=profile&uid=483ec3a0-7e0f-32e6-f357-ac9311204246http://example.com/profile/483ec3a0-7e0f-32e6-f357-ac9311204246/
但每次都会出现404错误。如果我尝试呼叫http://example.com/
或http://example.com/home/
我得到一个500错误
我的问题是什么?您需要另一个无参数重定向规则,并且需要向内部转发规则添加一些条件,以便
/index.php
不匹配:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+)&uid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php($|\ ) [NC]
RewriteRule ^ /? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&uid=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
看起来你遇到了无限循环的问题,导致500。这是因为最后两条路由规则没有跳过文件和目录 在
#内部转发
行上方插入此规则:
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
好的,谢谢。我没有收到500个错误,但仍然收到404个错误。@JohannLöwen如果您注释掉前三个重定向规则,是否转到
http://example.com/index.php?page=profile&uid=483ec3a0-7e0f-32e6-f357-ac9311204246
给你们一个404?是的,如果我把前三条规则注释掉,它就可以了。谢谢你们,你们太棒了。答案是你们两个答案的总和。谢谢你们,你们太棒了。答案是你两个答案的总和。