SEO sitewide redirect

Hello,

I am changing all my pages site wide and would appreciate some input if it is an okay idea to use a universal rule.

RewriteCond %{THE_REQUEST} [1]{3,9}\ /(.)\.php(.)\ HTTP/
RewriteRule ^(.*)\.php$ http://www.mywebsite.ca/$1/city$2 [R=301,L]

I am using a redirect for all my pages that are indexed by google ending with php to remove the extension and tell google the page has been moved. I am adding a city after each page to help with SEO and to avoid the use of an extra query field on every page. Then I am putting whatever query that existed onto the end of the link. I’ve tried it and it works but is it a good idea to go about this method or should I use individual pieces of code for each webpage ending in php?

Thanks,
Gman


  1. A-Z ↩︎

Thanks, gman!

# ----- RULE 1 -----
# Redirect to to www.mysite.ca when www is missing
RewriteCond %{HTTP_HOST} !^www.mysite.ca$ [NC]
RewriteRule .? http://www.mysite.ca%{REQUEST_URI} [L,R=301] 

# ----- RULE 2 -----
# Convert static city1 in url to dynamic url and redirect all .php pages to city1 url, lc=1 is first city 
# Let me assume Apache 2 to get rid of the /? after the start anchor
RewriteRule ^(.+)/city1$ [COLOR="Red"]/[/COLOR]$1.php?lc=1 [QSA,L]
# first, / leading the redirection CAN confuse Apache
# and make it go off to the physical root of your system
# so try to avoid that

# Then, the No Case flag will NOT work as expected in
# a RewriteRule (it only applies to the (.+) which matches
# one or more of ANY character so it's use is ridiculous, anyway).

# The QSA will preserve any pre-existing query string

RewriteCond %{REQUEST_URI} ^([COLOR="Red"](/?[/COLOR][a-z_][COLOR="Red"])[/COLOR]+)\\.php$

# You do NOT want to include the /? inside the second atom!
# It's just not there in Apache 2.x, anyway
RewriteCond %{QUERY_STRING} !lc=1
RewriteRule ^(.+)\\.php$ [COLOR="Gray"]http://www.mysite.ca/[/COLOR]$1/city1?%{QUERY_STRING} [R=301,L]

Delete the red and you can also delete the gray.

Regards,

DK

In addition, {THE_REQUEST} is a terrible Apache variable to use!

Regards,

DK

The $2 was coming from the second (.) in the condition statement. It was meant to get all the query that could possibly follow the ?. I will change all the (.). Thanks

References to the conditions are made using %, not $
i.e.

RewriteCond %{THE_REQUEST} [1]{3,9}\ /
(.) – that’s %1
\.php
(.
) – that’s %2
\ HTTP/

RewriteRule ^
(.*) – and thats $1
\.php$ http://www.mywebsite.ca/$1/city$2 [R=301,L]

:slight_smile:


  1. A-Z ↩︎

What is $2 supposed to be?
As it stands now you only have one atom in your rewriterule, namely (.*)
And that would be $1. I don’t see any $2.

Also, please refrain from using that dreaded ANYTHING atom (the (.*) thing), use more specific atoms instead, like \d for digits, \w for alphanumeric, etc

My mistake, the first rule should have been this. I copied in old code before it was changed.
RewriteCond %{HTTP_HOST} !^www\.mysite\.ca$ [NC]
RewriteRule .? http://www.mysite.ca%{REQUEST_URI} [R=301,L]

I’ve had a read through the tutorial which by the way was very well done. Could you please take a look at what I’ve written. I also tried using // for comments as was suggested but it comes up as a server error but # works.

# ----- RULE 1 -----
# Redirect to to www.mysite.ca when www is missing
RewriteCond %{HTTP_HOST} !^www.mysite.ca$ [NC]
RewriteRule ^(.*)$ http://www.mysite.ca/$1 [L,R=301] 

# ----- RULE 2 -----
# Convert static city1 in url to dynamic url and redirect all .php pages to city1 url, lc=1 is first city 
RewriteRule ^/?(.+)/city1$ /$1.php?lc=1&%{QUERY_STRING} [nc,L]
RewriteCond %{REQUEST_URI} ^((/?[a-z_])+)\\.php$
RewriteCond %{QUERY_STRING} !lc=1
RewriteRule ^(.+)\\.php$ http://www.mysite.ca/$1/city1?%{QUERY_STRING} [R=301,L]

Thanks,
Gman

Thanks for clarifying that. I’ve picked up on mod_rewrite in the last week and there are a lot of things that aren’t entirely clear. I’ll put the %1 and %2 where they should be. Using {The_Request} was something that I found consistently used in forums to redirect GoogleBot. What would be a better way in dealing with this?

Gman

Check out the article by dklynn, section “Redirect TO New Format”.