Forum Moderators: phranque

Message Too Old, No Replies

htaccess externel Redirect Problem

htaccess external redirect

         

reshadat

6:57 pm on Mar 9, 2009 (gmt 0)

10+ Year Member



Hi,
I'm trying to achieve pretty urls for my site. here is my htaccess file:

RewriteEngine On

RewriteRule ^tags/(.*)$ e107_plugins/tagcloud/tagcloud.php?$1 [L]


This internally redirects mysite.com/e107_plugins/tagcloud/tagcloud.php?test to mysite.com/tags/test.

Now I want to redirect visitors from old url to new. I tried the following line:


RewriteRule ^e107_plugins/tagcloud/tagcloud.php?(.*)$ /tags/$1 [R,L]

But it does not work. Also, after adding this line, the new urls also don't work. Please help!

Thanks

g1smd

7:39 pm on Mar 9, 2009 (gmt 0)

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



*** This internally redirects mysite.com/e107_plugins/tagcloud/tagcloud.php?test to mysite.com/tags/test. ***

No. That's not what it does. You're looking at it back to front.

It takes a URL request for example.com/tags/<something> or for www.example.com/tags/<something> and rewrites the request to internally fetch the content from /e107_plugins/tagcloud/tagcloud.php?<something> instead of where the URL suggests that it might be stored.

.

R makes a 302 redirect. Use R=301 instead.

A redirect should include the domain name in the target URL.

RewriteRule cannot see parameters. Use an additional RewriteCond looking at QUERY_STRING here.

reshadat

4:21 am on Mar 10, 2009 (gmt 0)

10+ Year Member



Thanks for the explanation g1smd. I searched the forums and got my answer. Here is my htaccess file:


RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /e107_plugins/tagcloud/tagcloud\.php\?([^&]+)\ HTTP/
RewriteRule ^e107_plugins/tagcloud/tagcloud\.php$ http://mysite.com/tags/%1? [R=301,L]

RewriteRule ^tags/(.*)$ e107_plugins/tagcloud/tagcloud.php?$1 [L]

Thanks for the help. Its a great forum.

g1smd

9:10 am on Mar 10, 2009 (gmt 0)

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



That looks like it should do the job; redirects first, rewrites last. However do test it with another parameter added on the end, as it looks like it might fail in that case.

I would also insert a general site-wide non-www to www 301 redirect after that specific 'parameter' redirect, and before the rewrite, to fix up all of the other non-www URLs on the site.

RewriteBase /
is the default and can usually be omitted.

Oh, and the reason the initial code didn't work is because it created an infinite redirect - rewrite - redirect - rewrite - etc loop. The fix was for the redirect to be invoked only for direct client requests (by examining THE_REQUEST).