Forum Moderators: phranque
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?
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]
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]
# 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.
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]
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
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?
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]
RewriteCond %{QUERY_STRING} ^start=([0-9]+)$ [NC]
RewriteCond %{REQUEST_URI} !.*home2/.* [NC]
RewriteRule ^^index\.php$ [mydomain.com...] [L]