Friendly URL redirection

Discuss practical ways rearrange URLs using mod_rewrite.

Friendly URL redirection

Postby selvam_pec51 » Wed Jul 30, 2008 6:14 am

Hi Everyone,

I want to redirect friendly URLs to Actual URLs.

Friendly URL - http://example.com/phone-devices
Actual URL - http://example.com/servlet/CS?pagename=phone

I want to write the redirect rules for the following 2 requirements
1. http://example.com/phone-devices should be rewritten to http://example.com/servlet/CS?pagename=phone by apache [Internal Redirection Required]. Friendly URL should be retained in the browser. Please find below the rewrite rule which i have written for this requirement

# Part 1
RewriteCond %{HTTP_HOST} example.com
RewriteRule ^/phone\-devices/?$ /servlet/CS?pagename=phone [R,P,L]

The above working fine.

2. http://example.com/servlet/CS?pagename=phone should be redirected to http://example.com/phone-devices in the user's browser so that this friendly URL will again use the Part 1 rewrite rule to get the page from the server and to maintain the friendly URL in the browser. The rewrite rules which i have written for this requirement are

# Part 2
RewriteCond %{QUERY_STRING} pagename=phone
RewriteRule ^/servlet/CS http://example.com/phone-devices/? [R]

When I am putting the above two in the httpd.conf file, it starts looping between the URLs. Could anyone please tell whether the above requirements are possible? please tell where i made mistake?
selvam_pec51
 
Posts: 7
Joined: Wed Jul 30, 2008 5:41 am

Postby richardk » Wed Jul 30, 2008 4:12 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?pagename=phone(&.*)?$ [NC]
RewriteRule ^/servlet/CS$ http://example.com/phone-devices/? [R=301,L]

RewriteRule ^/phone-devices/?$ /servlet/CS?pagename=phone [PT,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby selvam_pec51 » Wed Jul 30, 2008 10:25 pm

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?pagename=phone(&.*)?$ [NC]
RewriteRule ^/servlet/CS$ http://example.com/phone-devices/? [R=301,L]

RewriteRule ^/phone-devices/?$ /servlet/CS?pagename=phone [PT,L]

Thank you very much for your code. But I am getting an apache 404 error when i enter the URL "http://example.com/phone-devices". This means that the rewrite to "http://example.com/servlet/CS?pagename=phone" url is not happening.

I have tried again by changing the [PT] flag into [p] flag. In this case Redirection is happening, but leading to looping. "The request doesn't complete at all". I have also noted that here redirection is happening instead of rewrite[Internal Redirect].
selvam_pec51
 
Posts: 7
Joined: Wed Jul 30, 2008 5:41 am

Postby richardk » Fri Aug 01, 2008 3:44 pm

This is a bit of a hack
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} !^(.*&)?dontrewrite(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?pagename=phone(&.*)?$ [NC]
RewriteRule ^/servlet/CS$ http://example.com/phone-devices/? [R=301,L]

RewriteRule ^/phone-devices/?$ /servlet/CS?pagename=phone&dontrewrite [P,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Thank You Very Much!!!

Postby selvam_pec51 » Sun Aug 03, 2008 9:50 pm

Hi Richard!!

Thank you very much for your effort towards my query.

Your code is working fine.

I have removed dontrewrite concept from your first set of rules as it requires further redirect.

Could you please explain about this dontrewrite concept?

Thanks once again!!!

Regards,
Selva
selvam_pec51
 
Posts: 7
Joined: Wed Jul 30, 2008 5:41 am

Postby richardk » Wed Aug 06, 2008 3:15 pm

I have removed dontrewrite concept from your first set of rules as it requires further redirect.

Could you please explain about this dontrewrite concept?

It was to stop the loop. It was added only to the proxy requests so that they would not get redirected.
I have tried again by changing the [PT] flag into [p] flag. In this case Redirection is happening, but leading to looping. "The request doesn't complete at all". I have also noted that here redirection is happening instead of rewrite[Internal Redirect].

Without the dontrewrite RewriteCond the mod_rewrite is the same as the previous version.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby selvam_pec51 » Wed Sep 17, 2008 1:15 am

Hi Richard,

Again there is a problem with the rewrite rule. The rewrite which you have given working perfectly in all the environments except in the live environment. The live environment host name is www.example.com. Please find below the rewrite rules which i have rewritten based on the new hostname

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} !^(.*&)?dontrewrite(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?pagename=phone(&.*)?$ [NC]
RewriteRule ^/servlet/CS$ http://www.example.com/phone-devices/? [R=301,L]

RewriteRule ^/phone-devices/?$ /servlet/CS?pagename=phone&dontrewrite [P,L]

It seems rewrite from the friendly URL to actual URL is not happening ie [P,L] flag at the last rewrite rule is not working. Could you please tell me what might be the reason for this?

Thanks in advance.

Regards,
Selvandhan M
selvam_pec51
 
Posts: 7
Joined: Wed Jul 30, 2008 5:41 am

Postby richardk » Fri Sep 19, 2008 7:12 am

Does the redirect part work?
Does the live server have mod_proxy enabled?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby selvam_pec51 » Mon Sep 22, 2008 4:35 am

1. The redirect part is working fine. But [internal] rewrite part is alone not working.

2. In order to make the proxy [P] flag to work, we need to enable in mod_proxy.so module. I have enabled it dynamically using 'LoadModule'.

But Rewrite is not happening.

Regards,
Selvandhan M
selvam_pec51
 
Posts: 7
Joined: Wed Jul 30, 2008 5:41 am

Postby richardk » Tue Sep 23, 2008 11:54 am

Try PT again instead of P. (If PT work you will not need mod_proxy).

What module is running /servlet/CS?
Where is the servlet configuration?
Where is the mod_rewrite?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 24 guests

cron