homepage Welcome to WebmasterWorld Guest from 23.22.59.252
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
redirect php extensions to no extension
junglesnail

5+ Year Member



 
Msg#: 3961636 posted 10:52 am on Jul 29, 2009 (gmt 0)

Hello,
I've been reading threads on this forum on this subject and .htaccess tutorials but it seems nothing works for me.
I want to have extensionless pages.
For this, I have in my .htaccess file ( on my "localhost" for the moment ) the following lines:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php

And here comes the problem. i want to avoid duplicate content so if a user accesses page1.php i want a permanent redirect to page1. Nothing that i found works for me.

If i cannot make a generalized redirect for this i'm thinking to take each page individual and do something like :
RewriteRule ^page\.php$ http://www.example.ro/page [R=301,L]

the problem is i have over 70 links and growing :(.

Can you please help me with this ?

Thank you.

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3961636 posted 11:12 am on Jul 29, 2009 (gmt 0)


RewriteEngine On
#
# Internally rewrite extensionless URL to corresponding .php
# file unless the URL exists as a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
#
# Externally redirect (only) direct client requests for .php URLs to extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]

Note that it should not be necessary to check for "file exists" on an extensionless URL request, because all files on your server should have an extension. The only check needed is for "directory exists", since an extensionless URL may exist as a directory if a slash is added (automatically) to that URL.

Jim

junglesnail

5+ Year Member



 
Msg#: 3961636 posted 11:19 am on Jul 29, 2009 (gmt 0)

Jim,
It works.
A lot of thanks for helping out.

Could there be another duplicate content if i also fix the www and non-www to be the same?

junglesnail

5+ Year Member



 
Msg#: 3961636 posted 11:20 am on Jul 29, 2009 (gmt 0)

*sorry i ment *duplicate content issue

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3961636 posted 12:05 pm on Jul 29, 2009 (gmt 0)

Redirecting non-www to www (or the reverse) will *prevent* a duplicate-content issue.

If you do this, I suggest ordering the rules with the .php-extension to extensionless URL redirect first, the domain redirect second and the extensionless-to-php internal rewrite last. This will prevent multiple redirects in the case of wrong-domain/file.php, and prevent 'exposing' the internal .php filepath to the client as a URL in the case of an extensionless URL request to the wrong domain.

Jim

junglesnail

5+ Year Member



 
Msg#: 3961636 posted 12:13 pm on Jul 29, 2009 (gmt 0)

I will
Thanks a lot Jim

junglesnail

5+ Year Member



 
Msg#: 3961636 posted 9:00 am on Jul 30, 2009 (gmt 0)

I forgot to tell you. I'm actually moving a site from .html to .php
I also have to redirect the old html files to the new extensionless.
Where should i place them ?
Can the rules order be :
1).php-extension to extensionless URL redirect
2)the domain redirect
3)internal rewrite
4) redirect from old html to the new extensionless page by page.

Am i getting this right ?

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3961636 posted 2:58 pm on Jul 30, 2009 (gmt 0)

No, your redirects must precede your internal rewrite. Further, page-by-page redirects are most-specific, and so should precede the domain redirect.

So your list above would be better ordered as 1-4-2-3 or 4-1-2-3. "4" and "1" are likely mutually-exclusive, so they can be in either order -- or even 'mixed together' if that makes more sense from a "self-documenting-code" standpoint.

Jim

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3961636 posted 7:24 pm on Aug 1, 2009 (gmt 0)

If "going extensionless" for the .html URLs is merely removing the .html part from the URL, you could simply add a local OR to your .php URL redirect:
\.php becomes \.(php¦html?)

However, your words "page by page" maybe hint that the old and new names are not similar.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved