Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Help

         

UsingApache

10:27 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



Hi,

I'm trying to redirect only if the URL is *exactly* like the following:

Redirect From
[mydomain.com...] (where xx can be any number)

Redirect To
[mydomain.com...]

NOTE: I do NOT want to redirect similar URL's like
"http://mydomain.com/index.php" or
"http://mydomain.com/index.php?start=xx&end=yy"

I've tried a bunch of different ways, but every time the URL is converted correctly, I get stuck in an infinite redirect loop. Any help please?

jdMorgan

11:00 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got both a URL and an attached query string to handle there, which is the likely cause of your problem...

In example.com/.htaccess, use:


RewriteCond %{QUERY_STRING} ^start=([0-9]+)$
RewriteRule ^index\.php$ http://example.com/index.php/subfolder/index.php?start=%1 [R=301,L]

Note that it's a fairly bad idea to add additional path info after a 'filetype' in a URL, so I'd recommend that you get rid of that redundant "index.php" at the start of the path if at all possible.

Jim

UsingApache

4:09 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Thanks so much for the response. Unfortunately, once again it works but gets stuck in a redirect loop.

I don't know why I didn't mention this in my first post, but it may be important: I'm actually working off of a subfolder from my root, so in reality I'm trying to go from:

[mydomain.com...]

To:
[mydomain.com...]

Not sure if that makes a difference (when applying your code, I just included the extra subfolder in the 2nd part of the RewriteRule). Also, the RewriteBase rule is commented out.

As for the extra index.php you mentioned, it is NOT necessary so I am able to get rid of that from the redirect.

[edited by: UsingApache at 4:21 pm (utc) on June 11, 2009]

UsingApache

4:15 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



<snip>

[edited by: jdMorgan at 6:28 pm (utc) on June 11, 2009]
[edit reason] Deleted copyrighted material [/edit]

UsingApache

6:41 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



Below is what I've added to my .htaccess file located in the subfolder from root: /joomla15.

# KM Rule to remove www and to redirect mydomain2 to mydomain
RewriteCond %{HTTP_HOST} ^www.mydomain$ [NC]
RewriteRule ^(.*)$ [mydomain...] [R=301,L]
RewriteCond %{HTTP_HOST} ^www.mydomain2$ [NC]
RewriteRule ^(.*)$ [mydomain...] [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain2$ [NC]
RewriteRule ^(.*)$ [mydomain...] [R=301,L]

# KM Rule to redirect /joomla15/index.php?start=xx to /joomla15/home2/index.php?start=xx
RewriteCond %{QUERY_STRING} ^start=([0-9]+)$
RewriteRule ^index\.php$ [mydomain...] [R=301,L]

The first part was simply to remove www and redirect a 2nd domain we have to our new domain. The 2nd part is the redirect attempt that is causing the loop.

jdMorgan

7:30 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your second ruleset should precede the first ruleset -- always put the most-specific redirect rules first (the ones that affect the fewest URLs) followed by least-specific redirect rules last. This avoids 'chained' or 'stacked' multiple redirects if more than one thing is wrong with the requested URL.

Follow your redirect rules with all of your internal rewrite rules, again in order from most-specific to least specific. Order the rewrites like this avoids unexpected operation. And ordering all of the redirects ahead of all of the rewrites prevents any redirect from 'exposing' a previously-rewritten filepath as a URL on the Web.

Also, that first ruleset can be replaced with one single rule for efficiency and corrections are needed for completeness:


RewriteCond %{HTTP_HOST} ^(www\.mydomain¦(www\.)?mydomain2) [NC]
RewriteRule ^(.*)$ http://mydomain/$1 [R=301,L]

Note that the end-anchor has been removed to prevent an FQDN or appended port number from defeating the rule.

Unfortunately, none of this has anything to do with the problem, and I suspect that the installation of Joomla in /home2 doesn't realize that it is in a sub-subdirectory (or it wasn't properly installed in that sub-subdirectory) and therefore is redirecting back to the subdirectory.

Having looked at the Joomla code before, there is nothing in the 'standard' code that would cause this redirection loop. Their mod_rewrite code is awful, but it does not contain anything that would interact with your redirect. Therefore, it must be something in the Joomla *scripts* that does a redirect 'back' to an undesired URL, which then triggers your rule again, and results in a loop.

BTW, to greatly improve the performance of Joomla, find any of its rules where RewriteConds testing -d and -f exist. If the RewriteConds are all 'ANDed' together, or if they are all 'ORed' together, then the -f/-d test RewriteConds can be moved so that they are executed last.

This avoids two unnecessary calls to the operating system's file manager, and potentially, two unnecessary physical disk read operations per HTTP request, when those operations are not required because the other RewriteConds are not met (AND case) or are already met [OR case]. In short, when logically possible, RewriteConds testing the filesystem should always be done last.

If you haven't already done so, you may wish to install the Live HTTP Headers add-on for Firefox/Mozilla browsers so that you can 'watch' the HTTP transactions between your browser and your server. Also, take a look at your server error log in conjunction with your server access log. Doing these things often makes it much easier to spot bad redirects and rewrites.

Jim

UsingApache

8:29 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



Wow, that was quite a bit of info...thanks. It'll take me a little time to decipher it and start applying some of those suggestions!

So, it seems joomla is causing the problem, but perhaps there is a workaround?

If joomla sends me to /joomla15/home2/index.php?start=6, is there any way via .htaccess for the URL to display as /joomla15/index.php?start=6 without actually redirecting, but rather just for display purposes?

UsingApache

9:08 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



Hmmm, I played around with the .htaccess file and it seems I found the portion of the code that causes the redirect loop. The first block is our redirect block and the 2nd is a snippet of the code that joomla requires in the .htaccess file.

RewriteCond %{QUERY_STRING} ^start=([0-9]+)$
RewriteRule ^index\.php$ [mydomain.com...] [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/¦\.php¦\.html¦\.htm¦\.feed¦\.pdf¦\.raw¦/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

Any suggestions on how to modify this?

[edited by: UsingApache at 9:55 pm (utc) on June 11, 2009]

UsingApache

11:02 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



Ahh, I figured out the problem. I had to change the first block of code to:

RewriteCond %{QUERY_STRING} ^start=([0-9]+)$ [NC]
RewriteCond %{REQUEST_URI} !.*home2/.* [NC]
RewriteRule ^^index\.php$ [mydomain.com...] [L]

g1smd

1:09 pm on Jun 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** Any suggestions on how to modify this? ***

Jim gave several suggestions above that will improve performance.

Also be aware that using multiple .* patterns in one line will be very inefficient, and can actually be omitted from the code directly above.