Forum Moderators: phranque

Message Too Old, No Replies

Changing the SE request for dynamic URL to static URL

         

Robo

5:30 pm on Jun 9, 2008 (gmt 0)

10+ Year Member



In an earlier thread, with help from this forum I managed to get the main part of the SEF URLs working properly on a new site.
[webmasterworld.com...]

I am now redoing an old site, and everything is tested and ready with exception of the rewrite rule to redirect SE requests for the old dynamic URLs to the new static URLs. Saw several threads about it, including Jim’s, but cannot get that one to work.

We have only a handful of pages without clear links between dynamic and static URL. The internal static-dynamic rewrite rules just mention each page individual, I try to do the same here with the dynamic-static rules:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?act=5\ HTTP/
RewriteRule ^\?act=5$ http://www.example.org/nice-url? [R=301,L]

Also not working:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?act=5\ HTTP/

Or
RewriteCond %{THE_REQUEST} ^\?act=5\ HTTP/

It should be very simple but what am I doing wrong here?

jdMorgan

10:07 pm on Jun 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule cannot "see" any query strings. Therefore, your rule will never match. RewriteRule only looks at the URL-path, and not at any query appended to it. Correct your code as follows:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?act=5\ HTTP/
RewriteRule ^$ http://www.example.org/nice-url? [R=301,L]

Jim

Robo

2:32 pm on Jun 10, 2008 (gmt 0)

10+ Year Member



As usually, your solution solved it. After that, it was just a simple question of copying and pasting to do all the pages. Thanks very much.

Just a last question (I hope):
Almost all links will be to www.example.org/? (homepage) or www.example.org/?act=xx (other pages). That is now taken care of by this last rule.

However, when directly entering www.example.org/index.php (home page) and www.example.org/index.php?act=xx in the browser they still show the old dynamic URL (no redirects). I don't think many people -if any- will have links using those URLs, but is it normal to externally redirect them also as precaution or just leave it as I have it now (only the \? without index\.php\? variations) to keep your htaccess as small as possible.

More specifically, does an SE register or store the home page as www.example.org and automatically get the index.php or actually store it as www.example.org/index.php? If the later, that URL might need its own redirect as well probably?

Thanks again
Robo

jdMorgan

5:53 pm on Jun 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can solve this easily with one more rule:

RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^index\.php$ http://www.example.org/?%1 [R=301,L]

Put this *after* your more-specific, per-query redirects.

Note that in the case where the query string is blank, the trailing "?" on the replacement URL will be discarded -- as it should be.

Jim

Robo

10:20 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



That one did not work.

Order of rules in .htaccess:
- first ALL the internal rewrites from SEF url to dynamic. All rules end with [L]
- then all the external redirects for SEs still using the normal dynamic URLS, ending with [R=301,L]
- finally your last suggestion for getting the old .../index.php? links redirected as well, also ending with [R=301,L]

In Firefox, the URL changed from example.org/index.php?act=12 to the SEF url (example.org/nice-url), but did not load the page. Firefox gives an error message "Firefox has detected that the server is redirecting the request for this address in a way that will never complete"

Besides, entering a SEF URL now does not result in an internal rewrite anymore but ends as well with the same error message.

Changing it to ^ (.*) makes that the URL changes from .../index.php?act=12 to …/?act=12, but still the page does not load, the SEF does not work either, and both still give the same error message?

It is strange that the SEF URLs also stop work. Should not the internal rewrite rule with its ending in [L] stop the process before ever getting to the final rule?

jdMorgan

10:56 pm on Jun 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, it was late...

The rule must only be invoked for direct client requests for "index.php", and not as the result of an internal rewrite. So we must examine %{THE_REQUEST} -- The original request header as received from the client:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php(\?([^\ ]*))?\ HTTP/
RewriteRule ^index\.php$ http://www.example.org/?%2 [R=301,L]

Note that while [L] stops mod_rewrite processing, it only does so for the current pass through mod_rewrite. In .htaccess, if any RewriteRule is invoked, mod_rewrite is re-started to check for further rewrites or access restrictions on the new URL. This behavior can be confusing, but it makes sense if you think about it from a security standpoint.

In addition, if an external redirect is invoked, then the client will issue an entirely-new HTTP request, restarting *all* server-side directive processing.

Jim

Robo

5:58 am on Jun 12, 2008 (gmt 0)

10+ Year Member



That did it, great.

Actually, the thing not working made me think hard on what it was trying to do and why it should or should not work, which is better than just blindly copying a solution. Thought I start getting close to understanding, but clearly not close enough yet.

All double-checked and all rules are working fine.
Thanks again
Robo

g1smd

11:26 am on Jun 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I always list all of the redirects before all of the rewrites.

dusky

12:05 am on Aug 10, 2008 (gmt 0)

10+ Year Member



I wonder if Jim, jdMorgan or someone has a clue, I have mod_rewrite and all the nice URLs working OK, the problem is old bookmarks of people and old links on other sites still list the old dynamic URLs.

For example:
For our news I have displayarticle(.*).html for all articles, the dynamic URL for that is index.php?name=News&file=article&sid=$1
Which I redirect like this and worked OK:

RewriteRule ^displayarticle([0-9]+)\.html$ index.php?name=News&file=article&sid=$1 [L,NC,NS]

Now the problem I am having is in Google WMT telling me I have duplicate Titles, I can understand why, it's because when you paste an URL from my dynamic URLs on the browser it still takes you to the article, example mysite.com/index.php?name=News&file=article&sid=37788

The site suffered a penalty for three years now and I think is because we changed all dynamic URLs to SEF URLs when the old dynamic URLs were the ones which held most of the PR and had all backlinks, the site was PR8 going on PR9 and a leader in its field to a mere PR6 now with 90% of traffic wiped out :( but I am not bitter!

I wanted to 301 redirect index.php?name=News&file=article&sid=37788 to displayarticle37788.html for anyone pasting it on the browser or clicking on it on other sites, which will in time 301 redirect all old bookmarked and backlinks to the current URL.

I tried the above suggestion, I am not having any luck, could someone give me a pointer?

jdMorgan

1:21 am on Aug 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must redirect direct client requests for the bookmarked/indexed dynamic URLs to the corresponding static URLs using code like that in my third post above, and modifying the RewriteCond pattern and the substitution URL to suit. More info here [webmasterworld.com]

Jim