Forum Moderators: phranque

Message Too Old, No Replies

query string redirects

redirect query strings to form nicely looking URLS

         

harvinder

10:54 am on Aug 5, 2008 (gmt 0)

10+ Year Member



Hello people,

I know this questions may have been asked many time on the forum and everywhere and also got answered, but do not seem to be able to find a working solution to match my needs.

I have a lot of pages on my website which use query strings and would like to redirect them to show meaningful URLS, such as:

user types: www.domain.com/article/15/?page=12
should redirect to: www.domain.com/article/15/12/

user types: www.domain.com/something/2/?page=21
should redirect to: www.domain.com/something/2/21/

user types: www.domain.com/another/33/22/?page=50
should redirect to: www.domain.com/another/33/22/50/

So, in a way and general expression that would handle any query string containing the word "page=" and if it is something else it should not do anything, because I have some ajax calls to some files which use query strings and I do not want those to stop working.

Also, would it be okay to have internat links on the webpages I am generating to have hyperlinks same as the ones I marked above as "user types", or should I change them to be like the redirected ones? Actually I was wondering if it is going to slow the website down or something?changing them might not be a big problem, but I just thought if things can be fine without me changing those links of each files.

harvinder

12:56 pm on Aug 5, 2008 (gmt 0)

10+ Year Member



I forgot to mention one thing.

I was also getting a little confused on the idea of redirect. Actually if the above starts working for the users and the query string are adjusted to make the url look better. What will happen if the user enters the nicely formed URL, so:

instead of: www.domain.com/another/33/22/?page=50
user types this: www.domain.com/another/33/22/50/

will the user be able to see the pages fine? Its like thinking upside down, hope this makes sense!

Please help me out I am very confused.

jdMorgan

1:13 pm on Aug 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please review this previous thread: Changing Dynamic URLs to Static URLs [webmasterworld.com]. I believe that if read carefully, it will answer your questions and point you in the right direction.

Jim

harvinder

2:57 pm on Aug 5, 2008 (gmt 0)

10+ Year Member



Man, that was something similar. However I was more on the lines of a regular expression that would allow changes done for many URLs i.e.

user types: www.domain.com/article/15/?page=12
should redirect to: www.domain.com/article/15/12/

user types: www.domain.com/something/2/?page=21
should redirect to: www.domain.com/something/2/21/

user types: www.domain.com/another/33/22/?page=50
should redirect to: www.domain.com/another/33/22/50/

But the example only shows example for redirects in the corrent folder. I tried doing this:

Options +FollowSymlinks
RewriteCond %{QUERY_STRING} page=(.+)
RewriteRule (.*)index\.php [domain.com...] [R=301,L]

but this did not seem to work either, it was even redirecting [domain.com...] to [domain.com...]

which was wrong and also I could not get rid of the "?page=12", another thing was that it did not worked on an example that used "\?page=12" without an index.php file in front of it.

Could you please help?

jdMorgan

3:23 pm on Aug 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know whay you included "index.php" in your pattern. Also, you'll need to use THE_REQUEST to prevent a loop as the result of interaction with the existing rewrite to your script. Try:

Options +FollowSymlinks
RewriteEngine on
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)+\?page=([^&]+)&?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)+)$ http://www.example.com/$1%2? [R=301,L]

Jim

[edited by: jdMorgan at 3:36 pm (utc) on Aug. 5, 2008]

harvinder

3:36 pm on Aug 5, 2008 (gmt 0)

10+ Year Member



Hello again, that seemed to work, although it was changeing the requesting at server side as well. I only wanted to show a nice looking url in the users browser. I am using the below methd in the index.php to get the query argument:

$page = $_GET["page"];

I wonder if this previously supplied rules change the way I catch the query argument, because at the moment I am getting 404 not found error, but the url formation is fine.

jdMorgan

3:39 pm on Aug 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please re-read the thread I cited above, carefully, and in its entirety. There are three steps to using friendly URLs, and it sound like you are missing one of them.

Jim

harvinder

3:41 pm on Aug 5, 2008 (gmt 0)

10+ Year Member



I should have mentioned this case as well:

would the above supplied rules provide rules work if the user types in the nicely formatted URL at the first place? I mean if the user types in www.domain.com/another/33/22/50/ will he/she get the correct page because in the background my php file should be invoked like this should redirect to: www.domain.com/another/33/22/?page=50 to generate that page.

jdMorgan

3:54 pm on Aug 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your question is answered in the thread I cited: Changing Dynamic URLs to Static URLs [webmasterworld.com]. It took me six hours to write that post, and all of these issues are covered in it. Please read it so that I will not have to repeat myself. There are three steps to using friendly URLs, and you are still missing at least one of them.

If you have specific questions not already answered in that thread, please post them here.

Jim