Forum Moderators: phranque

Message Too Old, No Replies

Updating Wordpress rewrite

Updating Wordpress rewrite to reflect site move.

         

EddNCFC

9:19 am on Jun 20, 2012 (gmt 0)

10+ Year Member



Following on from this thread [webmasterworld.com...] I now have a .htaccess file pointing an old url to a new url like this:

Old version: www.exampl.com
New version: www.example.com

using this:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I have a news section which uses WordPress. In the WordPress root directory I have the standard WordPress rewrite set up so that on my site I have a nice looking URL like this:

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

# END WordPress

However, this does not redirect to the new site and does not convert non www to www. How would I add in a line to point to the new site and non www to www and still retain the original redirect?

g1smd

7:14 pm on Jun 20, 2012 (gmt 0)

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



What order are the rulesets in?

Redirects should be placed before rewrites.

RewriteEngine On
should appear only once.

Delete the
IfModule
tags.

EddNCFC

7:38 am on Jun 21, 2012 (gmt 0)

10+ Year Member



This appears in a .htaccess file in the WordPress directory, not the root directory. It is located in root/news/

RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]

I need to apply a 301 redirect to the above code.

So, reading your reply it would look like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]

lucy24

9:05 am on Jun 21, 2012 (gmt 0)

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



If you want to throw people into screaming fits, quote this bit of WP boilerplate.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]


It translates as: "Every time you get any request of any kind whatsoever, detour to check whether it's a request for something that physically exists, and if not, send the user along to the index page." Request for a directory that doesn't exist because it's the product of a rewrite? Index page. Request for an image whose name you inadvertently capitalized in the HTML? Index page. Request for robots.txt with some error in the address? Index page.

At an absolute, rock-bottom minimum, the rule should be expressed as something like

RewriteCond %{REQUEST_URI} !/news/index\.php
RewriteRule \.php /news/index.php [L]

But it's still way too broad. Why does everyone in the world merit your index page? Only rewrite the ones that have requested specific URLs that are actually in use. Otherwise you'll get someone asking for

/paintings/rats/qfhqbqpisvdpxhgo.html

(google tried this on me recently, probably because I'd triggered their "check for soft 404s" routine) and you really do not want to serve them any kind of content. Other than your custom 404 page.

However, this does not redirect to the new site and does not convert non www to www.

It doesn't redirect to anywhere. The rule is set up as a Rewrite, not a Redirect. Different critters entirely.

If you wanted the index.php rule to be a redirect, you would have to do two things: add the complete protocol and domain, and append the R=301 flag, like so:

RewriteRule \.php$ http://www.example.com/news/index.php [R=301,L]

Yes, you need the L flag with redirects. Counter-intuitive, but that's the way it is. (Unless, ahem, your name is jdmorgan and you really do mean "Redirect eventually, but we've got some other stuff to take care of first.") If you leave off the R=301 flag it defaults to 302. If you use the flag but leave off the protocol and domain, the server uses whatever it was given-- with or without www., with or without extraneous port number.

EddNCFC

9:25 am on Jun 21, 2012 (gmt 0)

10+ Year Member



# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.php$ http://www.example.com/news/index.php [R=301,L]
</IfModule>

# END WordPress

This didn't work, I received a page doesn't exist error.

EddNCFC

9:46 am on Jun 21, 2012 (gmt 0)

10+ Year Member



I still need to do this to pick up the nice looking url:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]

However, I need to 301 old and nice looking URLs to new nice URL's.