Forum Moderators: phranque

Message Too Old, No Replies

Strange re-write help

mod_rewrite

         

jpxavier

3:19 am on Jul 30, 2009 (gmt 0)

10+ Year Member



I have been struggling with this problem for the past week.
i am trying to redirect these urls:
http://www.example.com/register/def/abc/123
and
http://www.example.com/register/def/abc/123/
to
http://www.example.com/register.php/def/abc/123

Add .php to the first part of the uri segment.
[abc.com...] will redirect to
[abc.com...]

Is this possible? It appears simple but whatever I try is putting this server to infinite loop or it is adding .php to the last segment.

Any help is so much appreciated...

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



Why are you doing this? It isn't strictly necessary to 'expose' the ".php" in the URL by using a redirect, and changing the URL will incur a ranking loss from anywhere from a few weeks up to a year.

An internal rewrite might be more appropriate.

Since you did not post your code, I cannot comment on the cause of the loop other than to say that you must exclude the 'output' of the redirect or rewrite so that it does not match the 'input' pattern. If it does match, then a loop is the natural result. Usually, making the rule pattern more specific or using a RewriteCond to explicitly exclude the output path will easily solve this problem.

(A search here on WebmasterWorld for "rewrite loop" and/or "redirect loop" will turn up hundreds of previous threads on this subject.)

Jim

jpxavier

12:00 am on Jul 31, 2009 (gmt 0)

10+ Year Member



Thanks for the response.
You are correct. What I am trying to do is an internal redirect without exposing the .php
Here is what I have in the .htaccess file:
The problem is trying to match the first segment between // after the server name. .php is always added to the last segment.

<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ $1.php/$2 [L]
</IfModule>

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



Well, the problem is that the rewriterule pattern uses the greedy, promiscuous, and inefficient-when-occurring-multiple-times-in-a-pattern ".*" subpattern. Remember that ".*" matches anything, nothing, everything, and as much as possible. Therefore, everything but the final slash-prefixed part of the requested URL-path is matched into $1, giving the behavior you describe.

Furthermore, there is nothing in the pattern to reject paths that already have had .php added in, so this rule will loop on itself (in .htaccess, mod_rewrite processing is re-started whenever any rule has matched).

This modified pattern will be better with respect to fixing those problems, but I can't be sure what other URLs your site might use, so no guarantees:


RewriteRule ^([^/.]+)/([^/]+/[^/]+)/?$ $1.php/$2 [L]

Also, note that you would be much better off to avoid duplicate-content problems by picking either the final-slash version of the 'friendly URL' or the version without the slash. Then 301-redirect the non-preferred version to the preferred version, and rewrite only that preferred version to your script.

For every page or 'resource' on your site, there should be one and only one unique/canonical URL that can be used to reach it directly. All non-canonical variations of protocol (http vs. https), subdomain (e.g. 'www.'), domain, FQDN, port number, URL-path, and query string (if applicable) should be redirected to the canonical URL.

For example, many sites have a home page that can be reached at any of these URLs:
example.com/
example.com./
example.com:80/
example.com.:80/
www.example.com/
www.example.com./
www.example.com:80/
www.example.com.:80/
example.com/index.html
example.com./index.html
example.com:80/index.html
example.com.:80/index.html
www.example.com/index.html
www.example.com./index.html
www.example.com:80/index.html
www.example.com.:80/index.html

Whichever of those URLs is "correct" in the eyes of the Webmaster, it is competing with 15 identical copies of itself for links and ranking!

Jim

jpxavier

2:34 am on Jul 31, 2009 (gmt 0)

10+ Year Member



Jim-
I do not know where you find time to respond so quickly.. I do appreciate helping me out.
your suggestion, led me to the following to account for possible combinations. Is there a way to generalize this ?
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ $1.php/$2/$3/$4/$5 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ $1.php/$2/$3/$4 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ $1.php/$2/$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1.php/$2 [L]
RewriteRule ^([^/.]+)/?$ $1.php [L]

jdMorgan

3:18 am on Jul 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This handles all the cases covered by those rules:

RewriteRule ^([^/.]+)((/[^/]+)*)/?$ $1.php$2 [L]

Regular expressions are quite powerful... :)

Jim

jpxavier

3:35 am on Jul 31, 2009 (gmt 0)

10+ Year Member



No doubt about it!

It worked like a charm!..
Thanks a lot..