与其他网址htacces重写规则冲突 [英] htacces rewrite rule conflict with other url

查看:141
本文介绍了与其他网址htacces重写规则冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下htaccess文件。问题是,而转到page2.php中将其将page1.php中这样page3.php去page1.php中。

 选项+了FollowSymLinks -MultiViews
RewriteEngine叙述上
的RewriteBase / bookshow /


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

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


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


的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^ /] *)/ $ page1.php中?名称= $ 1 [L,NE,QSA,NC]

的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^ /] *)/ $使page2.php?ID = $ 1 [L,NE,QSA,NC]

的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^([^ /] *)/ $ page3.php?猫= $ 1 [L,NE,QSA,NC]
 

解决方案

随着巴拿马杰克说,服务器不能简单的的猜测的该重写需要的页面已经被重定向后,会出现完全是因为图案为每个三个规则是的完全一样的。因此,第一条规则的总是的执行,并没有什么东西会。

要解决这个问题,请使用以下code来代替:

  RewriteEngine叙述上

的RewriteBase / bookshow /

#第1步:重定向所有pagex.php ......新的URI?

的RewriteCond%{QUERY_STRING}猫=([^ \ S&功放;] +)$ [NC]
重写规则^ $ page1.php中CAT /%1? [R = 302,NE,L]

的RewriteCond%{QUERY_STRING}名=([^ \ S&功放;] +)$ [NC]
重写规则^ $则page2.php名称/%1? [R = 302,NE,L]

的RewriteCond%{QUERY_STRING} ID =([^ \ S&功放;] +)$ [NC]
重写规则^ page3.php $ ID /%1? [R = 302,NE,L]

#步骤2:重写猫/东西,名称/东西,ID /某事的实际网页

重写规则^猫/([^ /] *)/ $ page1.php中猫= $ 1和; R [L,NE QSA,NC]
重写规则^名/([^ /] *)1 / $ page2.php中将名称= $&安培; R [L,NE,QSA,NC]
重写规则^ ID /([^ /] *)1 / $ page3.php ID = $&安培; R [L,NE,QSA,NC]
 

新解:

第一步的,我们正在检查该页面被什么查询字符串的访问。如果存在匹配,重定向到新的URI。

例如, page1.php中?猫=你好将重定向到猫/你好

然后,在第二个步骤,我们重写这些新的URI到已经存在的页面,并追加&安培; R 的查询字符串,所以我们避免重定向循环。这&放大器;:R 基本上是多余的,就像一个虚拟的

例如,猫/你好将在内部重写为猫page1.php中你好=&放大器;?:R

更新时间:!我已删除的RewriteCond%{} REQUEST_FILENAME -f 的RewriteCond%{} REQUEST_FILENAME - 不需要这些 - ð从code为好。随意把他们回来,如果你曾经降落了需要他们。

I have the Following htaccess file. the problem is while goto page2.php its going to page1.php like this page3.php goes to 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. 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.

For example, page1.php?cat=hello will redirect to 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. This &r is basically redundant, and acts like a dummy.

For example, cat/hello will internally rewrite to 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. Feel free to put them back if you ever land up needing them.

这篇关于与其他网址htacces重写规则冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆