简体   繁体   中英

How Do I Avoid Loops in mod_rewrite?

This question has been edited for clarification:

I am trying to create "pretty URLs" that should disguise themselves as follows:

mysite.com/index.php?p=1 ==> mysite.com/site_one/index.php

mysite.com/aboutus.php?p=2 ==> mysite.com/site_two/aboutus.php

mysite.com/anypage.php?p=3 ==> mysite.com/site_three/anypage.php

As you can see, mod_rewrite should simply convert the "p" variable into a predetermined pseudo-directory (this directory does not exists...this is just an aesthetic rewrite).

Obviously, this creates a complication. When a user visits mysite.com/site_one/index.php, there should be a background transfer to mysite.com/index.php?p=1. This background transfer results in a loop that causes a server error.

I have attempted to prevent this loop by using the following code:

RewriteCond %{QUERY_STRING} !NON=
RewriteCond %{QUERY_STRING} ^(.*[&?])?p=1([&].*)?$
RewriteRule ^/?(.*)  /site_one/$1 [PT,L]


RewriteCond %{QUERY_STRING} !NON=
RewriteRule ^/?site_one/?(.*)  /$1?NON=1&p=1 [PT,L]

Unfortunately, this still causes a server error. Can anyone see where the code has gone wrong?

If you want, you could set a dummy query var behind the scenes. Try something like this:

RewriteEngine On

RewriteCond %{QUERY_STRING} !NON=
RewriteCond %{QUERY_STRING} ^(.*[&?])?p=1([&].*)?$
RewriteRule ^/?page.php  /main_site/ [R,QSA]

RewriteCond %{QUERY_STRING} !NON=
RewriteRule ^/?main_site/?(.*)  /page.php?NON=1&p=1&extra=$1 [QSA,L]

And just ignore NON from get/request in php.

I wound up having to work around this, as no htaccess method was helping me. I basically forwarded all values of "p" to the appropriate page and changing the variable "p" to the variable "s". I then told my web app that the value of "p" is equal to the value of "s" where "s" exists. I put this code in a file that communicates with the server, so it is present on all of my site's pages.

It's ugly and sloppy, but so is the reason I have to do it in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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