Forum Moderators: phranque

Message Too Old, No Replies

rewrite query string url to unique human friendly url

How to rewritten dynamic urls to static urls for the website using oscommer

         

skerobics

6:52 am on Apr 5, 2011 (gmt 0)

10+ Year Member



Hello i want to convert my dynamic urls to static urls as follows
www.mysite.com/course_desc.php?id=5 ->www.mysite.com/informatica-online-training.htm
www.mysite.com/course_desc.php?id=7 ->www.mysite.com/cognos-online-training.htm
www.mysite.com/course_desc.php?id=8 ->www.mysite.com/datastage-online-training.htm
www.mysite.com/course_desc.php?id=26 ->www.mysite.com/business-objects-online-training.htm


The problem here was i had another 50 urls of this kind.so i want to apply the loop method as given below.its working fine but the problem was,the new static urls not displaying my content but it displaying my header,left menu,right menu and my bottom.Its not fetching data from database driven data.Can anybody give me the solution

# Externally redirect direct client requests for old/unfriendly/query-stringed URLs to new friendly URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /course_desc\.php\?id=([0-9]+)\ HTTP/
RewriteCond %1>informatica ^5>(.+)$ [OR]
RewriteCond %1>cognos ^7>(.+)$ [OR]
RewriteCond %1>datastage ^8>(.+)$ [OR]
RewriteCond %1>business-objects ^26>(.+)$
RewriteRule course_desc\.php$ www.mysite.com/%1-online-training.htm? [R=301,L]
#
# Internally rewrite friendly URL requests to internal script filepaths
RewriteCond $1>5 ^informatica>(.+)$ [OR]
RewriteCond $1>7 ^cognos>(.+)$ [OR]
RewriteCond $1>8 ^datastage>(.+)$ [OR]
RewriteCond $1>26 ^business-objects>(.+)$
RewriteRule ^([^/\-]+)-online-training.htm$ /course_desc.php?id=1 [L]


I applied the the following individual code in my htaccess,i am able to rewrite and 301 them to the new clean static url.

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /course_desc\.php\?id=5\ HTTP/
RewriteRule ^course_desc\.php$ www.mysite.com/informatica-online-training.htm? [R=301,L]
RewriteRule ^informatica-online-training.htm$ /course_desc.php?id=5 [L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /course_desc\.php\?id=7\ HTTP/
RewriteRule ^course_desc\.php$ www.mysite.com/cognos-online-training.htm? [R=301,L]
RewriteRule ^cognos-online-training.htm$ /course_desc.php?id=7 [L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /course_desc\.php\?id=8\ HTTP/
RewriteRule ^course_desc\.php$ www.mysite.com/datastage-online-training.htm? [R=301,L]
RewriteRule ^datastage-online-training.htm$ /course_desc.php?id=8 [L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /course_desc\.php\?id=26\ HTTP/
RewriteRule ^course_desc\.php$ www.mysite.com/business-objects-online-training.htm? [R=301,L]
RewriteRule ^business-objects-online-training.htm$ /course_desc.php?id=26 [L]

jdMorgan

2:11 am on Apr 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your intent is to say that the second set of individual rules worked but the "lookup" rules at the top of your post did not work, then the problem is very likely related to the back-reference in the second rule:

RewriteRule ^([^/\-]+)-online-training.htm$ /course_desc.php?id=1 [L]

No matter what "friendly" URL is requested, this will always rewrite to your script with an id value of "1".

It is likely that you intended to use the back-reference "%1" to get the value from the preceding matched rewritecond instead of a literal character "1".

Jim

skerobics

6:53 am on Apr 12, 2011 (gmt 0)

10+ Year Member



I changed the second line to
RewriteRule ^([^/\-]+)-online-training.htm$ /course_desc.php?id=%1 [L]

when i typed this url hxxp://www.mysite.com/course_desc.php?id=5

Its redirected to the following url

hxxp://www.mysite.com/home/bigclass/public_html/folder.com/www.mysite.com/informatica-online-training.htm

additionally its written a 404 error
We hosted this wwebsite in our public_html/folder.com
my htaccess aslo resides at the same location
What may be the correct solution ?

jdMorgan

4:55 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> when i typed this url hxxp://www.mysite.com/course_desc.php?id=5

That URL is not affected by your
RewriteRule ^([^/\-]+)-online-training.htm$ /course_desc.php?id=%1 [L]
rule.

That URL is handled by the later rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /course_desc\.php\?id=5\ HTTP/
RewriteRule ^course_desc\.php$ www.mysite.com/informatica-online-training.htm? [R=301,L]


The form of a rewriterule is:
RewriteRule ^requested-URL-path$ new-URL-path-or-filepath


I'd suggest spending a week with the resources cited in our Apache Forum Charter if you want to save some time...

Jim

jdMorgan

5:03 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The appearance of that long server filepath in the redirected-to URL also implies that you have other rules executing before the redirect, and that at least one of those other rules is internally rewriting the URL to filepath before the redirect can take place.

Therefore, your code is exposing an internal server filepath as a URL, which is a rather serious problem.

To avoid this, make sure that all of your rewriterules end with an [L] flag. Make sure that all external redirects are invoked before any internal rewrites can be invoked. Make sure that you do not use the Redirect or RedirectMatch directives in any other .htaccess files -- if you use rewriterule for anything, then use it for everything.

Jim

skerobics

9:40 am on Apr 15, 2011 (gmt 0)

10+ Year Member



Thanks for your reply i really need some study before doing some experment with htaccess