...

View Full Version : Simple Redirection from PHP to HTML



theprivl
02-23-2009, 05:41 AM
alright.. well my problem lies in htacess. (I didnt know which forum to post it in)

anyways.. I have
http://website.com/about.php

but I want it to AUTOMATICALLY to be displayed as

http://website.com/about
NOT http://website.com/about/ or http://www.website.com/about/

how can I do this with my .htacess?


EXAMPLES:
YES: http://website.com/contact-us
NO: http://website.com/contact-us.php

YES: http://website.com/our-services
NO: http://website.com/our-services.php

abduraooft
02-23-2009, 07:43 AM
Google gives many results, http://www.google.com/search?q=htaccess+hide+php+extension

theprivl
02-23-2009, 12:59 PM
you solution does not solve my problem.

I need to AUTOMATICALLY redirect all my old links .. to new more SEF/SEO friendly links by simply taking away the extension, and eliminating the "www".

PS - I spent 5 hours on google. Nothing AUTOMATICALLY redirects. The content below works perfectly.. but it does not REDIRECT the user.. which makes it pretty pointless.



Options +FollowSymlinks
RewriteEngine on

# /about.php can be viewed as /about
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# this removes www from URL
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

abduraooft
02-23-2009, 01:12 PM
# this removes www from URL
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L] Try the following instead for no-www redirect.

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

theprivl
02-23-2009, 06:26 PM
Try the following instead for no-www redirect.

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


ok thanks.. but what about jut hiding the ".php" ?
Nothing works! I scoured google for the past 2 days.. nothing works!

theprivl
02-23-2009, 06:53 PM
WOW! I got it!


RewriteEngine on
#
## Internally rewrite extensionless file requests to .html files ##
#
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# and if it does not exist as a directory
RewriteCond %{REQUEST_fileNAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_fileNAME} !-f
# then add .html to get the actual filename
RewriteRule (.*) /$1.html [L]
#
#
## Externally redirect clients directly requesting .html page URIs to extensionless URIs
#
# If client request header contains html file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html\ HTTP
# externally redirect to extensionless URI
RewriteRule ^(.+)\.html$ http://www.example.com/$1 [R=301,L]

http://www.webmasterworld.com/apache/3371997.htm

theprivl
02-23-2009, 07:25 PM
so here is the final code..




RewriteEngine on

RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /$1.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php\ HTTP
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html\ HTTP
RewriteRule ^([^.]+)\.html /$1.php [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+htm\ HTTP
RewriteRule ^([^.]+)\.htm /$1.php [R=301,L]

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


here is what it does.. (i think)

.html ---> .php
.htm --> .php
php --> {removes ".php"}
also ... removes "www"

EXAMPLE OF USAGE

http://www.example.com/about.html
http://www.example.com/about.htm
http://www.example.com/about.php
http://example.com/about.html
http://example.com/about.htm
http://example.com/about.php

ALL AUTOMATICALLY redirect to..

http://example.com/about

if anyone wants to add on..please do so! . :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum