Welcome to WebmasterWorld Guest from 54.147.44.93

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

redirect php extensions to no extension

     

junglesnail

10:52 am on Jul 29, 2009 (gmt 0)

5+ Year Member



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

11:12 am on Jul 29, 2009 (gmt 0)

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




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

11:19 am on Jul 29, 2009 (gmt 0)

5+ Year Member



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

11:20 am on Jul 29, 2009 (gmt 0)

5+ Year Member



*sorry i ment *duplicate content issue

jdMorgan

12:05 pm on Jul 29, 2009 (gmt 0)

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



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

12:13 pm on Jul 29, 2009 (gmt 0)

5+ Year Member



I will
Thanks a lot Jim

junglesnail

9:00 am on Jul 30, 2009 (gmt 0)

5+ Year Member



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

2:58 pm on Jul 30, 2009 (gmt 0)

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



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

7:24 pm on Aug 1, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month