Forum Moderators: phranque

Message Too Old, No Replies

301 redirect

mod_alias vs mod_rewrite

         

brajno

11:49 am on Dec 2, 2009 (gmt 0)

10+ Year Member



Hi,
I was trying to do a 301 redirect like:

redirect 301 /rd-rss/index.php\sectionid= [site.com...]

but then I found this tread: [webmasterworld.com...]

so now I'm trying with:

RewriteCond %{QUERY_STRING} ^sectionid=$
RewriteRule ^rd-rss/index\.php\?sectionid=$ /index.php?option=com_rd_rss_clone&id=2 [R=301,L]

but nothing happens.

Any help would be greatly appreciated.

Thanks
Ben

g1smd

1:08 pm on Dec 2, 2009 (gmt 0)

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



RewriteRule cannot see query strings, so remove that part of the pattern test from the rule.

Add the protocol and domain to the target of the redirect to avoid redirecting to non-canonical URLs.

brajno

2:08 pm on Dec 2, 2009 (gmt 0)

10+ Year Member



something like this?

RewriteCond %{QUERY_STRING} ^sectionid=$
RewriteRule ^rd-rss/index\.php$ http://www.example.com/index.php?option=com_rd_rss_clone&id=2 [R=301,L]

[edited by: jdMorgan at 2:37 pm (utc) on Dec. 2, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:42 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That should work, as long as you have other working rewriterules in this .htaccess file. If not, then you will need to set up mod_rewrite and enable the rewrite engine using either both of these lines, or only the second of these lines:

Options +FollowSymLinks
RewriteEngine on

On some servers, the first line is required. On others it's not needed. And on still others, it's not allowed. Which of these is true depends on your server configuration, so you'll have to test.

Be sure to completely flush (delete) your browser cache before testing any new server-side code to avoid seeing stale results previously-cached by your browser.

Jim

brajno

4:15 pm on Dec 2, 2009 (gmt 0)

10+ Year Member



I tried all the combinations for

Options +FollowSymLinks
RewriteEngine on

but it should work with just RewriteEngine on. It doesnt.

I am doing this on a joomla 1.5 site and before that, I tried all possible components and plugins with no success. This is why I wanted to try with .htaccess.

Is it maybe because of some of these?

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]

jdMorgan

4:25 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That would depend on what order you put your rules in...

Put all of your external redirects (if any) first, followed by all of your internal rewrites. In each of those two groups, order your rules from most-specific to least-specific patterns and conditions. That is, rules which affect only one, or the fewest requests should go first, followed by those which affect more or most requests.

One exception is that HTTP_AUTH rule at the end. I don't know what it does, but if you have problems with authentication, then that rule should go first, its [L] flag should be removed, and all othe rules should very probably end with an [L] flag. I would say that all rules should always end with an [L] flag, but there are a few exceptions.

As usual, the joomla code is horribly inefficient; Put the last two rewriteconds first to improve your server response time.

Jim

jdMorgan

6:01 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After doing the above-described rule re-ordering and [L] flag adjustments, and making further efficiency improvements to Joomla's code, you'd get this:

# Copy HTTP:Authorization header value into HTTP_AUTHORIZATION variable for later use
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# Redirect /rd-rss requests if the "sectionid" variable is the only one in the query string and is blank
RewriteCond %{QUERY_STRING} ^sectionid=$
RewriteRule ^rd-rss/index\.php$ http://www.example.com/index.php?option=com_rd_rss_clone&id=2 [R=301,L]
#
# Rewrite specific URL requests to Joomla, but only if they do not resolve to existing files or directories
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^$¦(\.(php¦html?¦feed¦pdf¦raw))$¦/$¦^([^/]+/)*[^.]+$ index.php [NC,L]

The modified Joomla code now avoids two unnecessary calls to the OS to check for file- and directory-existsfor at least half of all requests. That is, these disk reads will never occur after mod_rewrite re-starts (as it always does after any rule is invoked in a .htaccess context) after any matching requests are initially rewritten to Joomla's index.php file, because the first RewriteCond will end the rule processing if the request has already/just been rewritten to the index.php path.

In other words, you should see at least a doubling of speed of this rule because it will never do the file- and directory-exists twice in a row as it used to. And you'll actually see even more that a 50% improvement, because only those requests which match the rule pattern will invoke the processing of any of the RewriteConds, so neither the 'NOT index.php' test RewriteCond nor the disk read RewriteConds will be processed unless the rule pattern matches.

Other than that, this modified Joomla code should be entirely equivalent in function to the original.

Important: Should you decide to try it, you will need to replace all of the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 11:04 pm (utc) on Jan. 7, 2010]

brajno

11:34 am on Dec 3, 2009 (gmt 0)

10+ Year Member



Thank you very much.

g1smd

12:29 pm on Dec 3, 2009 (gmt 0)

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



I think that code should be submitted to the Joomla dev team.

jdMorgan

2:00 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



They'll probably copy it eventually... I see signs that other such dev teams read here. So far, only OSC has attributed the code.

Since I don't use Joomla, it's probably better to make sure it works first, though... :)

Jim

raybb

10:30 pm on Jan 7, 2010 (gmt 0)

10+ Year Member



Just tried this streamlined code and it works great. Thanks!

Ray