301 + Delete extension + page redirect

New to mod_rewrite? This is a good place to start.

301 + Delete extension + page redirect

Postby flafson » Sat Jun 13, 2009 12:05 pm

Hey, great forum, very helpful.

From what i gathered myself, the .htaccess file i have does the following:
I moved my site from .com to .ca so i used a 301 redirect + i made sure everything goes to "www" + i deleted the .html extension of all pages on my site which i did manually by writing a redirect for every page (about 35 pages) and the last thing which i don't know how to do is i want one of the pages name to be changed so i want a redirect from the old page (which doesn't have an extension) to a new page which also doesn't have extension but has a different file name.

Ill copy paste the code i'm using now:

Code: Select all
RewriteEngine on

Rewritecond %{http_host} ^lilianalaser.ca [nc]

Rewriterule (.*)$ http://www.lilianalaser.ca$1 [r=301,nc]

Rewritecond %{http_host} ^lilianalaser.com [nc]

Rewriterule (.*)$ http://www.lilianalaser.ca$1 [r=301,nc]

Rewritecond %{http_host} www.lilianalaser.com [nc]

Rewriterule (.*)$ http://www.lilianalaser.ca$1 [r=301,nc]




RewriteRule ^Services$ Services.html [L]

RewriteRule ^Price-List$ Price-List.html [L]

RewriteRule ^About-Us$ About-Us.html [L]

RewriteRule ^faq$ faq.html [L]

RewriteRule ^Contact-Us$ Contact-Us.html [L]


RewriteRule ^Zerona-Liposuction-Toronto$ Zerona-Liposuction-Toronto.html [L]
RewriteRule ^Laser-Hair-Removal$ Laser-Hair-Removal.html [L]


RewriteRule ^Accent-Radio-Frequency$ Accent-Radio-Frequency.html [L]

RewriteRule ^Photo-Facial-Rejuvenation$ Photo-Facial-Rejuvenation.html [L]

RewriteRule ^Laser-Skin-Tightening$ Laser-Skin-Tightening.html [L]

RewriteRule ^Microdermabrasion$ Microdermabrasion.html [L]


RewriteRule ^Hydradermie-Guinot$ Hydradermie-Guinot.html [L]

RewriteRule ^Electrolysis$ Electrolysis.html [L]

RewriteRule ^Contact-Map$ Contact-Map.html [L]

RewriteRule ^Commercial-Resoures-One$ Commercial-Resoures-One.html [L]

RewriteRule ^Commercial-Resoures-Two$ Commercial-Resoures-Two.html [L]


RewriteRule ^Commercial-Resoures-Three$ Commercial-Resoures-Three.html [L]

RewriteRule ^Commercial-Resoures-Four$ Commercial-Resoures-Four.html [L]

RewriteRule ^Commercial-Resoures-Five$ Commercial-Resoures-Five.html [L]

RewriteRule ^Commercial-Resoures-Six$ Commercial-Resoures-Six.html [L]

RewriteRule ^Commercial-Resoures-Seven$ Commercial-Resoures-Seven.html [L]


RewriteRule ^Commercial-Resoures-Eight$ Commercial-Resoures-Eight.html [L]

RewriteRule ^Commercial-Resoures-Nine$ Commercial-Resoures-Nine.html [L]

RewriteRule ^Commercial-Resoures-Ten$ Commercial-Resoures-Ten.html [L]
RewriteRule ^Commercial-Resoures-Eleven$ Commercial-Resoures-Eleven.html [L]

RewriteRule ^Commercial-Resoures-Twelve$ Commercial-Resoures-Twelve.html [L]


RewriteRule ^Commercial-Resoures-Thirteen$ Commercial-Resoures-Thirteen.html [L]

RewriteRule ^Commercial-Resoures-Fourteen$ Commercial-Resoures-Fourteen.html [L]

RewriteRule ^Commercial-Resoures-Fifteen$ Commercial-Resoures-Fifteen.html [L]

RewriteRule ^Commercial-Resoures-Sixteen$ Commercial-Resoures-Sixteen.html [L]

RewriteRule ^Commercial-Resoures-Seventeen$ Commercial-Resoures-Seventeen.html [L]


RewriteRule ^Commercial-Resoures-Eighteen$ Commercial-Resoures-Eighteen.html [L]

RewriteRule ^Commercial-Resoures-Nineteen$ Commercial-Resoures-Nineteen.html [L]


The page I'm trying to change his name is the following:
Laser-Hair-Removal (.html) and it should be redirected to Laser-Hair-Removal-Toronto (.html)

Also, while we are at it, maybe (tell me what you think) it would be wiser to make a rule to change everything to lower case?

Thanks for the help!
flafson
 
Posts: 6
Joined: Sat Jun 13, 2009 11:49 am

Postby richardk » Sat Jun 13, 2009 3:23 pm

A shorter version
Code: Select all
Options +FollowSymLinks

RewriteEngine On

Rewritecond %{HTTP_HOST} ^lilianalaser\.ca$ [NC]
Rewritecond %{HTTP_HOST} ^(www\.)?lilianalaser\.com$ [NC]
Rewriterule .* http://www.lilianalaser.ca%{REQUEST_URI} [R=301,L]

RewriteRule ^(Services|Price-List|About-Us|faq|Contact-Us|Contact-Map)$ /$1.html [L]

RewriteRule ^(Zerona-Liposuction-Toronto|Laser-Hair-Removal)$ /$1.html [L]

RewriteRule ^(Accent-Radio-Frequency|Photo-Facial-Rejuvenation)$ /$1.html [L]
RewriteRule ^(Laser-Skin-Tightening|MicrodermabrasionZ|Hydradermie-Guinot|Electrolysis)$ /$1.html [L]

RewriteRule ^Commercial-Resoures-(One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven|Twelve)$ /Commercial-Resoures-$1.html [L]
RewriteRule ^Commercial-Resoures-(Thirteen|Fourteen|Fifteen|Sixteen|Seventeen|Eighteen|Nineteen)$  /Commercial-Resoures-$1.html [L]


The page I'm trying to change his name is the following:
Laser-Hair-Removal (.html) and it should be redirected to Laser-Hair-Removal-Toronto (.html)

What is the actual HTML file's name?
What is the URL that you want in the address bar of the browser?
Code: Select all
RewriteRule ^Address-Bar-URL$ /The-HTML-File.html [L]

If they visit the other URL, should the be redirected to the working URL? (Put the redirects first.)
Code: Select all
RewriteRule ^The-Other-URL$ /URL-To-Redirect-To [R=301,L]


Also, while we are at it, maybe (tell me what you think) it would be wiser to make a rule to change everything to lower case?

Do you have a problem with people using the wrong cased URL?
Do you have access to the httpd.conf file?
Do you have a server-side language (eg. PHP) available?

Should Resoures be Resources?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby flafson » Sat Jun 13, 2009 5:34 pm

Thank you so much for the fast reply.

Your code is working besides one case i noticed that it has missed. If i type lilianalaser.ca it doesn't redirect me to www.lilianalaser.ca

Kinda lost you in the page redirect part.
What i have right now is this:

RewriteRule ^(Zerona-Liposuction-Toronto|Laser-Hair-Removal)$ /$1.html [L]

Where the actual file name is Laser-Hair-Removal.html
And the new file name is Laser-Hair-Removal-Toronto.html

I get 404 if i type the wrong cased URL
I can't see on ftp httpd.conf file
I suppose the server supports PHP or other things, tell me what to look for and i can ask my host (they have a useful live chat for support)

OMG, you just found a spelling error there, funny, i had those files spelled wrong for like 2 years...
flafson
 
Posts: 6
Joined: Sat Jun 13, 2009 11:49 am

Postby richardk » Sun Jun 14, 2009 10:38 am

Your code is working besides one case i noticed that it has missed. If i type lilianalaser.ca it doesn't redirect me to www.lilianalaser.ca

Oops
Code: Select all
Rewritecond %{HTTP_HOST} ^lilianalaser\.ca$ [NC]

should be
Code: Select all
RewriteCond %{HTTP_HOST} ^lilianalaser\.ca$ [NC,OR]


Kinda lost you in the page redirect part.
What i have right now is this:

RewriteRule ^(Zerona-Liposuction-Toronto|Laser-Hair-Removal)$ /$1.html [L]

Where the actual file name is Laser-Hair-Removal.html
And the new file name is Laser-Hair-Removal-Toronto.html

Which is the one in the address bar of the browser?

Try
Code: Select all
# Redirect permanently /Laser-Hair-Removal to /Laser-Hair-Removal-Toronto.
RewriteRule ^Laser-Hair-Removal$ /Laser-Hair-Removal-Toronto [R=301,L]
RewriteRule ^(Zerona-Liposuction-Toronto|Laser-Hair-Removal-Toronto)$ /$1.html [L]


I get 404 if i type the wrong cased URL

But do visitors do it? If they don't do it then you might not need to do it. (Not that it's difficult.)

I can't see on ftp httpd.conf file

If it's shared hosting you probably won't have access to it.

I suppose the server supports PHP or other things, tell me what to look for and i can ask my host (they have a useful live chat for support)

Create a file called info.php and put the following in it
Code: Select all
<?php phpinfo();

What do you see when you visit it?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby flafson » Sun Jun 14, 2009 12:04 pm

PHP Version 5.2.6
And a huge list of whatever, the file is there and you can check it if you want to know something specific.

RewriteCond %{HTTP_HOST} ^lilianalaser\.ca$ [NC,OR]
That works great now, thanks.


# Redirect permanently /Laser-Hair-Removal to /Laser-Hair-Removal-Toronto.
RewriteRule ^Laser-Hair-Removal$ /Laser-Hair-Removal-Toronto [R=301,L]
RewriteRule ^(Zerona-Liposuction-Toronto|Laser-Hair-Removal-Toronto)$ /$1.html [L]

That one also did the trick, good job!

"But do visitors do it? If they don't do it then you might not need to do it. (Not that it's difficult.) "
I would say that probably not, but i wouldn't want to miss anyone over something silly like that... Most visitors wouldn't notice something like that and just assume the site is dead.

I also put a link to this forum on another forum which someone there told me about this forum, as a thanks to you :)
flafson
 
Posts: 6
Joined: Sat Jun 13, 2009 11:49 am

Postby richardk » Mon Jun 15, 2009 11:30 am

PHP Version 5.2.6
And a huge list of whatever, the file is there and you can check it if you want to know something specific.

That should work fine then.

"But do visitors do it? If they don't do it then you might not need to do it. (Not that it's difficult.) "
I would say that probably not, but i wouldn't want to miss anyone over something silly like that... Most visitors wouldn't notice something like that and just assume the site is dead.

Do you want to keep the mixed case URLs or do you want them to be lower case?
Do you have any other HTML files that need leaving alone?
Do you have a custom 404 page?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby flafson » Mon Jun 15, 2009 3:29 pm

The mixed case urls are fine as long as it will work both ways if someone types it wrong or something.

Not sure what you mean by other HTML files, i guess not.

And i don't have any custom 404 page, could probably redirect to the homepage.

Thanks
flafson
 
Posts: 6
Joined: Sat Jun 13, 2009 11:49 am

Postby richardk » Mon Jun 15, 2009 5:41 pm

Try adding
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([-a-z]+)/?$ /mr404.php?mr404=$1 [QSA,L]

to the end of your mod_rewrite.

And then create a PHP file, mr404.php, in your document root and put the following in it
Code: Select all
<?php

$correct_case = array(
   'services' => 'Services',
   'price-list' => 'Price-List',
   'about-us' => 'About-Us',
   'faq' => 'faq',
   'contact-us' => 'Contact-Us',
   'zerona-liposuction-toronto' => 'Zerona-Liposuction-Toronto',
   'laser-hair-removal' => 'Laser-Hair-Removal',
   'accent-radio-frequency' => 'Accent-Radio-Frequency',
   'photo-facial-rejuvenation' => 'Photo-Facial-Rejuvenation',
   'laser-skin-tightening' => 'Laser-Skin-Tightening',
   'microdermabrasion' => 'Microdermabrasion',
   'hydradermie-guinot' => 'Hydradermie-Guinot',
   'electrolysis' => 'Electrolysis',
   'contact-map' => 'Contact-Map',
   'commercial-resoures-one' => 'Commercial-Resoures-One',
   'commercial-resoures-two' => 'Commercial-Resoures-Two',
   'commercial-resoures-three' => 'Commercial-Resoures-Three',
   'commercial-resoures-four' => 'Commercial-Resoures-Four',
   'commercial-resoures-five' => 'Commercial-Resoures-Five',
   'commercial-resoures-six' => 'Commercial-Resoures-Six',
   'commercial-resoures-seven' => 'Commercial-Resoures-Seven',
   'commercial-resoures-eight' => 'Commercial-Resoures-Eight',
   'commercial-resoures-nine' => 'Commercial-Resoures-Nine',
   'commercial-resoures-ten' => 'Commercial-Resoures-Ten',
   'commercial-resoures-eleven' => 'Commercial-Resoures-Eleven',
   'commercial-resoures-twelve' => 'Commercial-Resoures-Twelve',
   'commercial-resoures-thirteen' => 'Commercial-Resoures-Thirteen',
   'commercial-resoures-fourteen' => 'Commercial-Resoures-Fourteen',
   'commercial-resoures-fifteen' => 'Commercial-Resoures-Fifteen',
   'commercial-resoures-sixteen' => 'Commercial-Resoures-Sixteen',
   'commercial-resoures-seventeen' => 'Commercial-Resoures-Seventeen',
   'commercial-resoures-eighteen' => 'Commercial-Resoures-Eighteen',
   'commercial-resoures-nineteen' => 'Commercial-Resoures-Nineteen'
   );

if(isset($correct_case[strtolower($_GET['mr404'])]))
{
   header('Location: http://' . getenv('HTTP_HOST') . '/'
          . $correct_case[strtolower($_GET['mr404'])],
          true, 301);
   exit();
}


header('Content-Type: text/html', true, 404);
?>
Your custom 404 page here.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby flafson » Mon Jun 15, 2009 7:35 pm

What is this code suppose to do?

When i try to enter the wrong case url i get a 404 error, no special page or anything.
flafson
 
Posts: 6
Joined: Sat Jun 13, 2009 11:49 am

Postby richardk » Tue Jun 16, 2009 10:18 am

Even all lowercase URLs?

Replace
Code: Select all
RewriteRule ^([-a-z]+)/?$ /mr404.php?mr404=$1 [QSA,L]

with
Code: Select all
RewriteRule ^([-a-z]+)/?$ /mr404.php?mr404=$1 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 127 guests

cron