3

I want to make all my URLs uniformly clean. Which means all my URLs have no extensions, nor trailing slash, and if a person does enter in a .php or trailing slash, it would simply redirect the person to the clean URL.

Example:

example.com/blog/file.php

and

example.com/blog/file/

would both redirect to

example.com/blog/file
MrWhite
  • 23,175
  • 4
  • 44
  • 71
user657847
  • 155
  • 3
  • 8
  • It depends: how are your files names name on the server? with or without the extension? – M'vy Apr 04 '11 at 16:03
  • If you're website is hosted on a Linux server with the Apache Mod-Rewrite moduled enabled, you can use .htaccess to redirect your files. This link[with information on how to format the file for your purpose ](http://www.isitebuild.com/301-redirect.htm) may help. – Yofat Apr 04 '11 at 03:01
  • I don't this applies to what I want, since I want to redirect all, not one page. – user657847 Apr 04 '11 at 03:28

3 Answers3

4

In .htaccess try this:

RewriteEngine on

#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
MrWhite
  • 23,175
  • 4
  • 44
  • 71
ryanve
  • 43,188
  • 26
  • 86
  • 125
3

My web host recently (Sept 2011) updated their Apache servers to a newer version of Apache, and my old .htaccess code to display extensionless URLs no longer worked. After a considerable amount of copying code (that probably worked with older versions of the Apache OS) and unsuccessfully testing, I was able to determine that the following code seems to work on my server, and several others:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]

Give that a try - the -Multiviews seemed to be the key element to get it to work.

MrWhite
  • 23,175
  • 4
  • 44
  • 71
0
# Turn off the need of directory slash redirecting
DirectorySlash Off

# Turn on RewriteEngine
RewriteEngine On

# Internally redirect request to php file
RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}\.php" -f
RewriteRule "(.*)" "$1.php" [L]

# External redirects
# The above line is used to determine whether a internal redirect is done or not.
# If it is internally redirected, REDIRECT_STATUS will become 200.
# RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"

# Redirect request with .php extension to extensionless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteRule "(.*)\.php$" "$1" [R=301]

# Redirect request with trailing slash to slashless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteCond "%{REQUSET_FILENAME} -d
RewriteCond "%{REQUSET_FILENAME} !-f
RewriteRule "(.*)\/" "$1" [R=301]
Anson Yeung
  • 198
  • 2
  • 12