homepage Welcome to WebmasterWorld Guest from 23.23.12.202
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

    
rewrite query string url to unique human friendly url
rewrite query strings
getoffmyisland



 
Msg#: 4236835 posted 2:42 pm on Nov 30, 2010 (gmt 0)

Hey,

I wanna do rewrites on a whole site. The url's are coded into flash so dont have resources to change the hard coded links in flash. So once again mod_rewrite have to come to the rescue.

These are examples of the rewrites i want to do rewrite/301 redirect:
http://www.example.com/view.php?id=1 -> http://www.example.com/industrial-products/
http://www.example.com/view.php?id=2 -> http://www.example.com/domestic-products/
http://www.example.com/suppliers.php?id=1 -> http://www.example.com/rosieries/
http://www.example.com/suppliers.php?id=2 -> http://www.example.com/pierre-roblin/

I've allready tried this rule:
RewriteRule ^industrial-products$ view.php?id=1 [L]

It works nice, but only half way, the problem being, the old url (www.example.com/view.php?id=1) is still accessible by the user and should also redirect to the new url

I think this is a unique problem, because there's no pattern how the redirects should be done, each id (or query string) must go to a different human friendly URL.

Can you pls redirect me to a solution as i can't find it on the site.

 

g1smd

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



 
Msg#: 4236835 posted 7:13 pm on Nov 30, 2010 (gmt 0)

You need another RewriteRule, this time coded with the [R=301,L] flags to redirect requests from one URL to another.

This new rule will also need a preceding RewriteCond looking at the THE_REQUEST. This will be used to match the query string originally requested by the browser.

The RewriteCond will stop the infinite redirect-rewrite loop that will otherwise ensue.

With the URLs you have at the moment, it is likely that you'll need a RewriteCond/RewriteRule pair for each particular query string.

If there are more than a few dozen URLs, the rules will be impossible to manage.

In that case, you create a special PHP script that can look up the new URL based on the requested parameters, when those parameters are an external request from a client, and you rewrite direct external client requests to point to this script. The script then issues the 301 header pointing to the right URL for the content.

The rewrite you already have in place is correct, but is only half of the solution.

getoffmyisland



 
Msg#: 4236835 posted 2:36 pm on Dec 1, 2010 (gmt 0)

i've come this far now,

RewriteRule ^industrial-products$ view.php?id=1 [L]

RewriteCond %{QUERY_STRING} ^id=1$ [NC]
RewriteRule ^view\.php$ http://www.example.com/industrial-products [R=301,L]

but the new url, http://www.example.com/industrial-products, still have the query string, ?id=1, at the end.

can u point me in the right direction to remove the query string from only the new url ?

Can i use a rule similar to this to remove the query string:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)$ http://www.example.com/$1? [L]
..but only want to remove it from the new url we created ?

i have 40 links i have to do this with, hope it's not too much, otherwise i'll have to use the special php script option, but only want to do as a last resort.

Then there's the rewrite loop to deal with as well as match the query string originally requested by the browser ?

g1smd

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



 
Msg#: 4236835 posted 9:58 pm on Dec 1, 2010 (gmt 0)

Test THE_REQUEST not QUERY_STRING. That will solve the redirect-rewrite loop problem.

Remove the query string in the redirect, simply by adding a question mark to the end of the target URL.

getoffmyisland



 
Msg#: 4236835 posted 10:25 am on Dec 2, 2010 (gmt 0)

RewriteRule ^industrial-products$ view.php?id=1 [L]

RewriteCond %{THE_REQUEST} ^id=1$ [NC]
RewriteRule ^view\.php$ http://www.example.com/industrial-products? [R=301,L]

You meen like this ?

This works now there exist two urls (one with query string and new
one)

It's not redirecting to the new one.

I've also tried adding this rule afterwards do redirect it:

RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^view\.php$ /industrial-products? [R=301,L]

..but it returns the error "the url is not valid and can not be loaded"

How can i get the rewrite to redirect to the new page at the same time ?

g1smd

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



 
Msg#: 4236835 posted 11:15 am on Dec 2, 2010 (gmt 0)

Test THE_REQUEST not QUERY_STRING.

THE_REQUEST contains the literal:

"
GET /somepath/somefile?someextension HTTP/1.1"

Use that to detect the query string the user actually requested.

There's tens of thousands of prior examples of how to use THE_REQUEST in this forum.

getoffmyisland



 
Msg#: 4236835 posted 3:47 pm on Dec 2, 2010 (gmt 0)

i unfamiliar with THE_REQUEST, i've checked out and tried some examples.

Is this going to be an additional rule, does it maybe look something like this:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /^industrial-products$
RewriteRule ^industrial-products$ view.php?id=1 [L]

Where does this come into play:
"GET /somepath/somefile?someextension HTTP/1.1"

Am i doing the rules in the right order, must the_request be at the end of the whole redirect ?

g1smd

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



 
Msg#: 4236835 posted 5:28 pm on Dec 2, 2010 (gmt 0)

Where does this come into play:
"GET /somepath/somefile?someextension HTTP/1.1"

That is the literal string the browser sends to the web server. You have to design your RegEx pattern to match that and extract just the requested parameters.

The rule will be a redirect, so it will need protocol and domain name in the target, and the [R=301,L] flags.

getoffmyisland



 
Msg#: 4236835 posted 9:39 am on Dec 6, 2010 (gmt 0)

Can you maybe give an example of how this regex pattern should match the redirect or point me to the right forum post, i've looked at other examples on this forum, but i'm really struggling to get it working.

jdMorgan

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



 
Msg#: 4236835 posted 3:41 am on Dec 7, 2010 (gmt 0)

The brute-force solution would be:

# Externally redirect direct client requests for old/unfriendly/query-stringed URLs to new friendly URLs
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /view\.php\?id=1\ HTTP/
RewriteRule ^view\.php$ http://www.example.com/industrial-products/? [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /view\.php\?id=2\ HTTP/
RewriteRule ^view\.php$ http://www.example.com/domestic-products/? [R=301,L]
#
#
# Internally rewrite friendly URL requests to internal script filepath plus query
#
RewriteRule ^industrial-products$ /view.php?id=1 [L]
#
RewriteRule ^domestic-products$ /view.php?id=2 [L]


However, if you have a lot of "view.php" id numbers, then you may wish to use a "lookup-table" approach like this for the sake of efficiency:

# Externally redirect direct client requests for old/unfriendly/query-stringed URLs to new friendly URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /view\.php\?id=([0-9]+)\ HTTP/
RewriteCond %1>industrial ^1>(.+)$ [OR]
RewriteCond %1>domestic ^2>(.+)$ [OR]
RewriteCond %1>discontinued ^3>(.+)$ [OR]
RewriteCond %1>custom ^4>(.+)$
RewriteRule ^view\.php$ http://www.example.com/%1-products/? [R=301,L]
#
# Internally rewrite friendly URL requests to internal script filepaths
RewriteCond $1>1 ^industrial>(.+)$ [OR]
RewriteCond $1>2 ^domestic>(.+)$ [OR]
RewriteCond $1>3 ^discontinued>(.+)$ [OR]
RewriteCond $1>4 ^custom>(.+)$
RewriteRule ^([^/\-]+)-products$ view.php?id=1 [L]

Although this code might look a bit complex, it is simply using RewriteConds to check only the "important unique part" of the requested path as extracted by the RewriteRule pattern, and then create a back-reference to the appropriate "replacement path-part" for that... i.e. replace numbers with words in the first case, and words with numbers in the second.

The ">" is an arbitrary character, used only to allow the RewriteCond's regex pattern to easily and unambiguously 'separate' the two values. It is used because it is unlikely to appear in your URLs. You could just as well use "~" or even a string like "<->". I use ">" simply because to me it implies concatenation and is short.

Jim

getoffmyisland



 
Msg#: 4236835 posted 11:07 am on Dec 9, 2010 (gmt 0)

Hey thanks for that, the top brute-force option works awesome. I've succesfully redirected/rewrited 16+ "view.php" querystring urls.

I've also tried an addition to this rule similar to this:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /view_rs\.php\?id=2&rn=2\ HTTP/
RewriteRule ^view_rs\.php$ http://www.example.com/electric-ovens.html? [R=301,L]
RewriteRule ^electric-ovens.html$ /view_rs.php?id=2&rn=2\ [L]

Note that there's two string items in the query string now, this works fine regarding url rewriting in the address bar and it is redirecting, but for some reason the page only displays the header and footer, without the product info generated through php (as if the query string is not doing its job after the rewrite)

Did i maybe do it wrong ? or is there another explanation why "query string is not doing its job right" after the rewrite/redirect.

Hope you understand what i'm trying to say.

jdMorgan

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



 
Msg#: 4236835 posted 12:33 am on Dec 14, 2010 (gmt 0)

What is the backslash after the query string in your last rule for? It says, "&rn=2\". It looks like that backslash "migrated" from its proper position escaping the period in the pattern. That rule should likely read:

RewriteRule ^electric-ovens\.html$ /view_rs.php?id=2&rn=2 [L]


Jim

getoffmyisland



 
Msg#: 4236835 posted 10:30 am on Mar 9, 2011 (gmt 0)

the backslash was my mistake

This rule works perfect now, it literally forces it brutally

Thanks alot for ur help !

getoffmyisland



 
Msg#: 4236835 posted 10:59 am on Mar 9, 2011 (gmt 0)

I have another addition to this thread

because of a site being poorly developed (custom CMS), when i implement a simple non www redirect to example.com, it

redirects to www.example.com/index.php?page=main even if there's no htaccess.

i've read and implemented ur previous posts about removing querystrings from the main page, www.example.com, but all

failed, because of the way the site is developed.

i found that i can manipilate this using our brute force rule.

..but now i just want to know how to get the below rule to display the main url (http://www.example.com/) only,

instead of htt://www.example.com/home.html ?


RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?page=main\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/home.html? [R=301,L]
RewriteRule ^home.html$ /index.php?page=main [L]

if i could get this done, and add a simple non www rule like this:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

it should work perfectly.

g1smd

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



 
Msg#: 4236835 posted 11:04 am on Mar 9, 2011 (gmt 0)

Your rule redirects index.php requests to home.html as coded.

Remove home.html from the redirect and from the rewrite, leaving ALL other characters intact.

getoffmyisland



 
Msg#: 4236835 posted 12:49 pm on Mar 9, 2011 (gmt 0)

it's that simple ! works beautifully, you guys rock !

g1smd

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



 
Msg#: 4236835 posted 1:13 pm on Mar 9, 2011 (gmt 0)

Now add a redirect from home.html to / for people who know the old URL.

RewriteRule ^home\.html http://www.example.com/ [R=301,L]

It goes before the non-www to www redirect code.

skerobics



 
Msg#: 4236835 posted 6:27 am on Mar 24, 2011 (gmt 0)

Hello, You guys are rocking,I applied these methods its perfectly working fine and i also want to add some more info to this discussion.These are my dynamic urls and i want to convert them to static urls as given.
www.mysite.com/course_desc.php?id=5 ->www.mysite.com/informatica-online-training-5.htm
www.mysite.com/course_desc.php?id=7 ->www.mysite.com/cognos-online-training-7.htm
www.mysite.com/course_desc.php?id=8 ->www.mysite.com/datastage-online-training-8.htm
www.mysite.com/course_desc.php?id=26 ->www.mysite.com/business-objects-online-training-26.htm

If i apply the the following individual rule,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$ [mysite.com...] [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$ [mysite.com...] [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$ [mysite.com...] [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$ [mysite.com...] [R=301,L]
RewriteRule ^business-objects-online-training.htm$ /course_desc.php?id=26 [L]

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$ [mysite.com...] [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]

Max134



 
Msg#: 4236835 posted 2:57 pm on Mar 26, 2011 (gmt 0)

I have a doubt about this post, guess that you want something like that


http://www.example.com/view.php?id=anynumber -> http://www.example.com/anytext/

what I mean in db you have the id, numbers, that it would be anynumber, for example view.php?id=12 , but the title of the subject is anytext, so you want to make the change.

How I can do that ?

An example is title of a post, it could be like that

http://www.example.com/42211/ /*equivalente to http://www.example.com/view.php?discussion=42211*/

/*but we want to show this: */
http://www.example.com/you-can-buy-the-new/

g1smd

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



 
Msg#: 4236835 posted 7:22 pm on Mar 26, 2011 (gmt 0)

There is no way for mod_rewrite to know the relationship between the name of the new post and the ID number of the post so you can't use a single rule to redirect a bunch of URLs here.

Mod_rewrite can be used when you have www.example.com/42211 and /view.php?discussion=42211 where both feature the same ID number.

It cannot be used when there is a name in the URL and only a number in the file path.

If you are prepared to have URLs like
www.example.com/42211-you-can-buy-the-new then there is a solution for the internal rewrite:

RewriteRule ^([0-9]+)-(.*) /view.php?id=$1&name=$2 [L]

but there is no simple solution for the redirects.

skerobics



 
Msg#: 4236835 posted 8:25 am on Apr 2, 2011 (gmt 0)

hello is there anybody to answer to my previous post

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