Forum Moderators: phranque

Message Too Old, No Replies

Redirecting dynamic pages

Redirection of URLs with query strings

         

gumbomudder

4:30 pm on May 31, 2006 (gmt 0)

10+ Year Member



I have a website built with dynamnic pages.
I recently redisigned the website and I had to reorganize even the folder structure.

Search Engines are still pointing to the old pages, how do I redirect them to the new ones?

Old pages looked like
www.mydomain.com/home.php?page=mypage

New pages look like
www.mydomain.com/index/template1.php?page=mynewpage

I have tried a 301 redirection with php, but that has to be inserted into the old home.php page, pointing to template1.php.
It works but I am loosing the variable so the redirection loads into the template the default page of my php scipt.
Therefore I am loosing the redirection to the specific variable/page which is of course relevant in order to redirect the Search to a page with the same content as the old one.

Any way to solve this problem?

jdMorgan

5:13 pm on May 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might consider going to static URLs, in order to improve your chances to rank well in search. Your question about query string redirection is answered indirectly as a side-issue in this tutorial on converting dynamic URLs to static URLs [webmasterworld.com].

Jim

gumbomudder

9:34 pm on May 31, 2006 (gmt 0)

10+ Year Member



The site ranks quite well even with dynamic urls so I am not concerned about obtaining better ranking, and I'd rather keep the dynamic urls.
Is it the only way to solve this problem?
I just need to redirect....

jdMorgan

8:58 pm on Jun 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's an example of previous threads on this subject: [webmasterworld.com...]

Jim

gumbomudder

10:54 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



I am afraid that post is beyond my knowledge.

I have tried the simple redirect in the .htaccess

Redirect /olddirectory/oldfile.php?page=mycontent [yoursite.com...]

This doesn't work and the people at the hosting company say I have to upgrade my plan because my current plan do not support mod rewrite.

Can this be made to work or is it wrong anyway.
This redirection must be done on many pages

Is there any way to obtain the same redirection with php?
I know this is not the forum to ask this question...

jdMorgan

9:36 pm on Jun 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to redirect URls with query strings attached, you'll need mod_rewrite. Hosting plans that support mod_rewrite are quite common and cheap. Look around before paying for an 'upgrade.'

Jim

marius26

4:44 pm on Jun 3, 2006 (gmt 0)

10+ Year Member



i got mod_rewrite and i need dynamic to dynamic

for example

domain/r.php?url=query
to
domain/out/out.php?out=query

what i want is htacces to redirect to same query bit user has typed but new url, hope you understand what i mean.

jdMorgan

1:44 am on Jun 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Keeping the query string is the default behaviour of mod_rewrite [httpd.apache.org], so no special query handling is needed.

Just


RewriteRule ^old_url$ http://www.example.com/new_url [R=301,L]

should work fine, with or without a query string.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

gumbomudder

9:54 pm on Jun 4, 2006 (gmt 0)

10+ Year Member



So if I have mode_rewrite enabled this should work...

RewriteRule ^http://mysite.com/home.php?page=oldpage$ [mysite.com...] [R=301,L]

Is this correct?

Now, two questions:
1) Can i repeat this for as many page as I need?
2) Do i have to add this line in the .htaccess file located in the root of my website, independently of where the page to redirect is located?

jdMorgan

3:11 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings must be handled separately, since they are not part of a URL, but rather, data attached to a URL to be passed to the resource at that URL:

RewriteCond %{QUERY_STRING} ^page=oldpage$
RewriteRule ^http://mysite.com/home.php$ http://www.example.com/indexes/newone.php?page=newpage [R=301,L]

> 1) Can i repeat this for as many page as I need?
Yes, but I wouldn't do more than say, 300 URLs on a busy site.

> 2) Do i have to add this line in the .htaccess file located in the root of my website, independently of where the page to redirect is located?
The code must be in the same directory as the requested local URL-path indicates, or in any HTTP-accessible directory above that directory. The server follows the requested URL-path and executes .htaccess directives along that path, so the code needs to be located where it will get executed.

If you have lots of page IDs to replace, you can use a little back-reference 'trick' to make a crude mapping function, reducing multiple RewriteCond/RewriteRule pairs to multiple RewriteConds followed by a single rule:


RewriteCond %{QUERY_STRING}<>newpage1 ^page=oldpage1<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>newpage2 ^page=oldpage2<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>newpage8 ^page=oldpage8<>(.+)$ [OR]
RewriteCond %{QUERY_STRING}<>newpage9 ^page=oldpage9<>(.+)$
RewriteRule ^http://mysite.com/home.php$ http://www.example.com/indexes/newone.php?page=%1 [R=301,L]

Note that the last RewriteCond MUST NOT have an [OR] flag on it. Also, the "<>" characters have no special meaning; They are simply a 'unique' character sequence used to delimit the requested query string from the replacement query string. You could use "~" or "FRED" or anything else, as long as it won't appear inside an actual requested query string.

Jim

gumbomudder

5:09 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



Hi Jim,

first of all let me thank you for your time.

Well I have tried what you suggest.
Here's what I done.

I added the following line to an .htaccess file which I placed in the root of my website.
----------------
RewriteCond %{QUERY_STRING} ^page=people$
RewriteRule ^http://www.mysite.com/home.php$ [mysite.com...] [R=301,L]
----------------

I have tried to access the page that should trigger the redirection but I get an internal server error

----------------
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
----------------

I get the same error whatever page I try to access on my website no matter if old ones or new ones.
If I rename (or remove) the .htaccess averything goes back to normal.
Looks like the server panicks when it reads my .htaccess.....

Any idea?
Did I get something wrong?

Thanks

jdMorgan

5:35 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What does your server error log say?

If you have no previously-working mod_rewrite rules, then you'll need to enable mod_rewrite before using it. This is done (typically) by adding


Options +FollowSymLinks
RewriteEngine on

ahead of any other mod_rewrite code in .htaccess.

If you check your server error log file, it will likely contain this same diagnosis.

Also note that if you are changing only the page URL and not the query string, then you can pass the existing query through mod_rewrite unchanged by using the simpler rule:


RewriteCond %{QUERY_STRING} ^page=people$
RewriteRule ^http://www.mysite.com/home.php$ http://www.example.com/indexes/about.php [R=301,L]

As noted above, the default behaviour is for mod_rewrite to pass queries unchanged.

For more information, please see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

gumbomudder

7:41 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



I have added the line to the .htaccess so now it looks like

-------
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=people$
RewriteRule ^http://www.mysite.com/home.php$ [mysite.com...] [R=301,L]
--------
Unfortunately I get the same error as before.
And I can't check the log file since I have no access to the server only to my website plan, I'm on a virtual hosting plan.

jdMorgan

7:52 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I missed another problem, and even cut-n-pasted it. Sorry, only so much time to post here...

Your RewriteRule pattern cannot contain "http://example.com" -- mod_rewrite requires a local URL-path only there.


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=people$
RewriteRule ^home\.php$ http://www.example.com/indexes/about.php?page=people [R=301,L]

The server error log is accessible through the "Control Panel" on almost any decent hosting plan. If the above modification doesn't fix your problem, I strongly suggest you find out how to access your server error log.

Jim

gumbomudder

8:18 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



I am sorry, it doesn't work.

I am on a cobalt raq server and the cobalt panel doesn't offer any control of the server, since I am not the server admin I have no password for it, just my site admin, users, email etc.

The people of the hosting co has been trying to force me to upgrade to Ensim, I don't know if this will help, if I will have access to the apache log file on Ensim.
Problem is I have to modify all the scripts with absolute server path if I change machine and that'd be a nightmare.
Well maybe it's time to do it, or evaluate a different provider with a better service.

Thanks so much for the help anyway.

jdMorgan

8:27 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do end up moving, then be aware that you can easily put the 'absolute server path' into a variable, and access it from within the scripts. See the Apache SetEnv directive. In this way, moving to a different server or changing the script directory structure means only a single change in your configuration file, rather than having to edit a large number of scripts.

Jim

gumbomudder

8:51 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



Thanks a lot for the suggestion!
You mean with a single line in the .htaccess i can solve all my problems without modifying the scripts?

Some of them are self-installing and required absolute server path at installation looking like
/home/site45/web/cgi-bin/
so you understand changing server path is a disaster, as I don't even have an idea of the use the script did of this path.

That'd be great if I could solve this problem!
And what about the path to perl? Can that be managed in the same way?

gumbomudder

10:17 am on Jun 6, 2006 (gmt 0)

10+ Year Member



Hi Jim,

I hope you can answer one more question.
I actually did a mistake in the path of the page to redirect since the home.php is not in the root but in a subfolder (en).
So the page I have to redirect is in
www.mysite.com/en/home.php?page=people
and not in
www.mysite.com/home.php?page=people

now, you say it is required local URL path so what do I have to change the path to?
The path you suggested in your post was:
^home\.php$
I don't understand how the path must be formed and the use of the slash between the name of the file home and the file extension .php.....
What am I supposed to use now?
^en/home\.php$ ?

Thanks

jdMorgan

1:06 am on Jun 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not try it? (Sometimes I only check in here once a day, and if no-one else is here to answer, it may take a long time to get any response.)

^en/home\.php$ is correct.

Jim