tags:

views:

33

answers:

4

Hey all,

I have a site I've written in php.

The addresses are as follows:
http://www.site.com/page.php

I'd like any request to:
www.site.com/page.php
or
www.site.com/page

To go to:
www.site.com/page/

(And force a www.)

Can anyone help me?

A: 
RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]

Maybe this will work.

Webarto
+1  A: 

For domain :

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

For your first directory :

RewriteRule ^/([^/]*)(\.php)? /$1/
Colin Hebert
This appears to be redirecting from `http://domain.com/page` to `http://www.domain.com`, not not preserving the page.
Robert
A: 

This should do what you wanted:

RewriteEngine On

RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?)\.php    [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST}   !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]

# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Tim Stone
A: 

Tim's answer is the closest, but it also does adds the trailing slash to images and css...

So taking Tim's answer, and slightly editing it gives us:

RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png)$ - [L]

RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?)\.php    [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST}   !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]

# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php

Which should work!

Richard