简体   繁体   English

.htaccess 重写规则多页

[英].htaccess Rewrite rule multiple pages

Have been trying to work this out for some hours now but not getting it right.几个小时以来一直在努力解决这个问题,但没有做好。

I have a site www.example.com/user.php?username=john I use .htaccess to make it pretty like so www.example.com/user/john I use the following .htaccess Rewrite rule and it works我有一个网站www.example.com/user.php?username=john我使用.htaccess让它看起来像这样www.example.com/user/john我使用以下.htaccess重写规则并且它有效

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./user.php?username=$1 [L]

The problem问题

I have a profile page like www.example.com/profile.php?username=john I also use the following .htaccess to make it good looking like www.example.com/profile/john我有一个个人资料页面,如www.example.com/profile.php?username=john我还使用以下.htaccess使其看起来像www.example.com/profile/john

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./profile.php?username=$1 [L]

Now, this doesn't work at all but when I remove the first rewrite rule (rule user.php) and place it after the second rewrite rule (rule profile.php) my profile page works, and vice versa,现在,这根本不起作用,但是当我删除第一个重写规则(rule user.php)并将其放在第二个重写规则(rule profile.php)之后时,我的个人资料页面有效,反之亦然,

So it's basically whatever rule I place first is what determines what page I can access with my URL.所以基本上是我首先放置的规则决定了我可以使用我的 URL 访问哪个页面。

I need it to work for both pages.我需要它为两个页面工作。 I will agree with you am not close to a beginner level at .htaccess rewrite.我同意你在.htaccess重写时还没有达到初学者水平。

Thanks in advance.提前致谢。

Could you please try following.能否请您尝试以下。 Since URLs are having their uniqueness so its better to check condition in their %{REQUEST_URI} value, I am checking if REQUEST_URI starts from user then go for 1 rule and if its starts with profile then go for another so there is no confusion(where I believe you tried very well kudos for that but since there is no segregation between conditions so hence its confusing Engine IMHO).由于 URL 具有其唯一性,因此最好检查其%{REQUEST_URI}值中的条件,我正在检查 REQUEST_URI 是否从user开始,然后是 go 对于 1 条规则,如果它以profile开头,那么对于另一个规则是 go 所以没有混淆(在哪里我相信你为此尝试了很好的荣誉,但由于条件之间没有隔离,因此它令人困惑的引擎恕我直言)。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/user/([\w-]+)/?$
RewriteRule ^.*$ /user.php?username=%1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/profile/([\w-]+)/?$
RewriteRule ^.*$ /profile.php?username=%1 [NC,L]

You can try below as well, checking first if the incoming request is neither a file nor a directory and then check for matching pattern.您也可以在下面尝试,首先检查传入请求是否既不是文件也不是目录,然后检查匹配模式。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([\w-]+)/?$ /user.php?username=$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([\w-]+)/?$ /profile.php?username=$1 [NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM