Forum Moderators: phranque

Message Too Old, No Replies

Why mod rewrite keeps adding .php exntention?

         

Dynuel

8:25 pm on Mar 31, 2012 (gmt 0)

10+ Year Member



I am changing my website's address to a new domain, and I want to use mod_rewrite to redirect all trafict from the old address to the new one.

.htaccess at the old address ( domain.com ) is:
--------------------------------------------
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]

--------------------------------------------

by what I understand it should transferr all url's to the new address ( domain.co.uk )


.htaccess at the NEW address ( domain.co.co.uk ) is:
-----------------------------------------
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]

RewriteRule ^contact$ contact.php
RewriteRule ^about$ about.php

ErrorDocument 404 /404.php

----------------------------------------

the first part is to redirect to www address if the entered url is without www

second part is to make short url's contact and about and the last bit for errors

------ THE PROBLEM ------
when I try to access domain.com/about
it should redirect to www.domain.com/about
BUT FOR SOME reason it redirects to www.domain.com/about.php --- I don't want php to be there! :/

and for some reason it works ok for domain.com/contact - it redirects to www.domain.com/contact

can you help me with it please?

lucy24

9:27 pm on Mar 31, 2012 (gmt 0)

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



For starters: Use example.com for all your examples. You can also say .net or .org or (I checked)* .co.uk if you need to distinguish between names. This is a universal WebmasterWorld rule. It is doubly important in the Apache forum because all other URLs turn into clickable links. We need to see what you really typed.

The Redirect at the old address is correct in principle: page for page, old domain to new domain. But depending on what URL is requested at the old domain, you may get into a tangle of multiple redirects at the far end.

Most obvious problem:

You have Rewrites without an [L] flag. That means your Rewrites will continue merrily through the rest of mod_rewrite, where they might meet anything. They might even get redirected again, which would be a calamity.

Requests at the old domain for
^(about|contact)(\.php)?$
with or without .php

should be redirected (301) straight to
www.example.co.uk/$1

Requests at the new domain for
^(about|contact)\.php$

with .php should be redirected (301) to
www.example.co.uk/$1

Once you've got everyone on the same URL page, you can proceed to the behind-the-scenes Rewrites.

Rewrites come after Redirects. Within each group, move from specific to general.


* http://www.example.co.uk/filename comes through loud and clear. And least in Preview :: fingers crossed ::