简体   繁体   English

htacces重写规则与其他URL冲突

[英]htacces rewrite rule conflict with other url

I have the Following htaccess file. 我有以下htaccess文件。 the problem is while goto page2.php its going to page1.php like this page3.php goes to page1.php. 问题是当goto page2.php转到page1.php时,例如page3.php转到page1.php。

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /bookshow/


RewriteCond %{THE_REQUEST} /page1\.php\?name=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]

RewriteCond %{THE_REQUEST} /page2\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,NE,L]


RewriteCond %{THE_REQUEST} /page3\.php\?cat=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,NE,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ page1.php?name=$1 [L,NE,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ page2.php?id=$1 [L,NE,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ page3.php?cat=$1 [L,NE,QSA,NC]

As Panama Jack stated, the server cannot simply guess which rewrite needs to occur after the page has been redirected, simply because the patterns for each of your three rules are exactly the same . 正如巴拿马杰克(Panama Jack)所说,服务器无法简单地猜测页面重定向后需要进行哪种重写,这仅仅是因为三个规则中的每个规则的模式都完全相同 As such, the first rule will always execute, and nothing else will. 这样,第一个规则将始终执行,而其他任何规则都不会执行。

To tackle this, use the following code instead: 要解决此问题,请改用以下代码:

RewriteEngine on

RewriteBase /bookshow/

# Step 1: Redirect all pagex.php?... to new URIs

RewriteCond %{QUERY_STRING} cat=([^\s&]+)$ [NC]
RewriteRule ^page1.php$ cat/%1? [R=302,NE,L]

RewriteCond %{QUERY_STRING} name=([^\s&]+)$ [NC]
RewriteRule ^page2.php$ name/%1? [R=302,NE,L]

RewriteCond %{QUERY_STRING} id=([^\s&]+)$ [NC]
RewriteRule ^page3.php$ id/%1? [R=302,NE,L]

# Step 2: Rewrite cat/something, name/something, id/something to the actual pages

RewriteRule ^cat/([^/]*)/?$ page1.php?cat=$1&r [L,NE,QSA,NC]
RewriteRule ^name/([^/]*)/?$ page2.php?name=$1&r [L,NE,QSA,NC]
RewriteRule ^id/([^/]*)/?$ page3.php?id=$1&r [L,NE,QSA,NC]

Further Explanation: 进一步说明:

In the first step , we're checking which page is being access with what query string. 第一步中 ,我们正在检查使用哪个查询字符串访问哪个页面。 If there is a match, redirect to the new URI. 如果匹配,则重定向到新的URI。

For example, page1.php?cat=hello will redirect to cat/hello . 例如, page1.php?cat = hello将重定向到cat / hello

Then, in the second step, we rewrite these new URIs to the already-existing pages, and append &r to the query string so we avoid a redirect loop. 然后,在第二步中,我们将这些新的URI重写到已经存在的页面中,并将&r附加到查询字符串中,从而避免了重定向循环。 This &r is basically redundant, and acts like a dummy. 这个&r是多余的,并且像一个虚拟角色。

For example, cat/hello will internally rewrite to page1.php?cat=hello&r . 例如, cat / hello将在内部重写为page1.php?cat = hello&r

Updated: I have removed RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d from the code as well - these are not needed. 更新:我也从代码中删除了RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d - RewriteCond %{REQUEST_FILENAME} !-d不需要这些。 Feel free to put them back if you ever land up needing them. 如果您需要它们,可以随时将它们放回原处。

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

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