Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule not working in htaccess (WordPress)

         

GabeCole

9:50 pm on Feb 11, 2011 (gmt 0)

10+ Year Member



Hi,

I've used redirects in the past with no problem but this is the first time I'm trying to use RewriteRule with no success for the past few hours.

I need to rewrite a bunch of URLs that changed in a website built with WordPress.
Basically, products that were formerly inside the 'generators' directory are now in the 'energy' directory. The URLs used to look like this:

h t t p://sitedomain.com/products/generators/55th/
and now:

h t t p://sitedomain.com/products/energy/55th/

This is what my htaccess looks like now:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress

RewriteEngine On
RewriteBase /
RewriteRule /products/generators(.*)$ h t t p://sitedomain.com/products/energy$1 [R=301,L]

This is not working at all.
Any ideas would be greatly welcome.
Thanks.

Gabe

ps: I changed 'http' for 'h t t p' in the post so that the system doesn't convert them to links automatically.

g1smd

10:11 pm on Feb 11, 2011 (gmt 0)

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



Use example.com to stop the forum autolinking the URLs.

Place the redirect code before the rewrite code otherwise the redirect code will expose internal filenames in the redirect.

RewriteEngine On - is needed only once in the entire file.

RewriteBase / - is the default and can be completely deleted.

The standard WP code is very inefficient. The -f and -d "exists" checks carry out two very slow disk reads and are invoked for every URL requested from the server. You should add extra RewriteCond exclusions before the -f and -d checks to stop those checks happening for URLs that obviously never get rewritten to your index.php file. Those include requests for images and stylesheets.

There's another WebmasterWorld thread with that example code discussed in detail.

Finally, remove the ifModule tags. You do not want code to silently fail when there is a problem with the server.

GabeCole

2:22 pm on Feb 12, 2011 (gmt 0)

10+ Year Member



Hi g1smd,

Sorry, I don't understand what you mean by "place the redirect code before the rewrite...". In my example there's not a redirect line. Just the default rewrite statements from WP plus one line where I'm trying to rewrite the URL for the changed directory.

I'll do a search for the code in the other WP post. It'd be very helpful to see what the actual code looks like (I'm not experienced to do it myself).

Thanks again.

g1smd

3:28 pm on Feb 12, 2011 (gmt 0)

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



The code with [R=301,L] is an external 301 redirect. This tells the browser to make a new request for a new URL.

The code with [L] is for a server internal rewrite. This maps the incoming URL request to the place on the server hard disk where that content resides.

It is too late to change the URL using an external redirect once you have already internally rewritten the request into the internal server filesystem.

The redirects must externally "fix" all URLs before you start any internal rewriting. Hence, change the order of the directives to fix this.

GabeCole

9:43 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



Hi,

I think this is the post you were referring to: [webmasterworld.com...]

I followed the suggestions for the default WP code and placed my redirect before those lines. However, the redirect is still not working, I keep getting a 404 error. This is what I have right now:

RewriteEngine On
RewriteBase /

RewriteRule /products/generators(.*)$ http://example.com/products/energy$1 [R=301,L]

RewriteCond %{REQUEST_URI} !(^/index\.php|\.(gif|jpe?g|png|ico|css|js)|^/robots.txt|/sitemap\.xml)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Your help is very appreciated.

g1smd

11:50 pm on Feb 14, 2011 (gmt 0)

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



RewriteRule cannot "see" the leading slash of the URL request.

Use ^products/generators(.*)$ instead.

GabeCole

3:01 am on Feb 16, 2011 (gmt 0)

10+ Year Member



That was it! Thank you so much.