Forum Moderators: phranque
First I've added:
RewriteCond %{HTTP_HOST}!^www\.*
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]
...to make sure that google doesn't get confused with www and non-www domains. Have I done the "[R=301,L]" part correct?
Second I've added:
RewriteRule ^(.*)\.htm$ $1.php [NC]
...All my pages are called index.php - but I'd rather the search engines see "index.htm" in my links. So I've told htaccess to really get the "index.php" page and not the "index.htm" page asked for - without telling the browser its doing this. Have I implemented this correctly?
Finally...
All my pages take the form index.php. But all my links refer to index.htm. When using the above rule, if someone requests a "index.htm" page they are actually getting the "index.php" page. But if they decide to type "index.php" they also see the page :( As I want the site to appear 100% static, I don't like this. So how can I stop people getting a page when they enter "index.php"?
I tried the following:
RewriteRule ^(.*)\.php$ - [G,L]
...but it seemed to stop the part outlined in step two from working! Again, I'm not sure what the "[G,L]" part is doing. Any help would be appreciated.
In order to prevent that, you need to use a special form of rule that checks the URL originally requested by the client.
# Redirect to canonical domain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# Rewrite <page>.htm requests to <page>.php
RewriteRule ^([^.]+)\.htm$ $1.php [NC,L]
#
# Redirect client requests for <page>.php to <page>.htm
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.php [NC]
RewriteRule \.php %1.htm [NC,R=301,L]
GET /page.php?some_query_string HTTP/1.1
or
POST /page.php HTTP/1.1
Jim