![](/img/trans.png)
[英]htaccess rewrite example.com/user.php?id=username to example.com/username
[英]Rewriting example.com/section.php?username=xyz to example.com/xyz/section
使用以下htaccess
,我已经能够将example.com/profile.php?username=xyz
重写为example.com/xyz
,
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
在上面添加以下内容,
RewriteRule ^([a-zA-Z0-9_-]+)/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/section/$ section.php?user=$1
没有解决example.com/section.php?username=xyz
到example.com/xyz/section
。
我在这里做错什么了吗?
首先:说话的方式宁可是相反的:你们就有了规则重写就像请求/xyz
内部以/profile.php?username=xyz
,而不是相反。
现在,如果要将内部的/xyz/section
类的请求重写为/section.php?username=xyz
,其中section
和xyz
是可变的,请尝试以下规则:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([^/]+)/?$ $2.php?user=$1
我对其进行了测试,并且可以正常工作。 尝试这个:
RewriteRule ^([a-zA-Z0-9_-]+)\/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)\/section\/$ section.php?user=$1
我所做的只是在URL中转义了/
,因为/
是正则表达式分隔符。
要在正确的目录中查找静态文件(图像,css)而不必写文件地址,请执行以下操作:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
在编写RageD建议的代码之前
(对不起,应该将其发布为评论,但我需要换行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.