Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite problem help please.

Dynamic URL rewrite to static and redirect dynamic to new URL

         

heartless09

5:38 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



Hi,

I have been reading all the forums and tutorials but I can't get my mod rewrite to work.
What I'm trying to do is this:
My URL look like:
[domeiname.com...] (second variable is optional)

I want to rewrite them to:
[domeiname.com...]

I got it to work for static URL to get the information from the right php page. But when I write the rules to redirect (301) the old URL to the new ones, I get 404 error.
.htaccess look like this.

RewriteEngine on
RewriteBase /testfolder

#Static to dynamic
RewriteRule ^titels/([A-Za-z0-9]+)/page_([0-9]+)\.html$ alfabatic.php?alfat=$1&start=$2 [L]
RewriteRule ^titels/([A-Za-z0-9]+)(/)?(index\.html)?$ alfabatic.php?alfat=$1 [L]

#dynamic to static
RewriteCond %{REQUEST_URI} ^.*alfabatic\.php$ [NC]
RewriteCond %{QUERY_STRING} ^alfat\=([a-zA-Z0-9]+)$ [NC]
RewriteRule ^.*$ titels/%1/? [R=301,L]

I hope someone sees the stupid mistake that I made somewhere...

Thanks in advance.

jdMorgan

11:33 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is likely one of recursion -- After mod_rewrite performs a rewrite, it re-starts (in order to enforce access restrictions). Therefore, your two rule-sets will work against each other, and create an 'infinite' loop.

For the 301 redirect, you must use the server variable %{THE_REQUEST} which contains the original (not rewritten) client request in order to avoid this situation. For your example, %{THE_REQUEST} would look like this:

GET http://www.example.com/testfolder/alfabatic.php?alfat=A&start=40 HTTP/1.1

By using this variable in the second (redirect) ruleset, you can avoid the redirection/rewrite deadloop, and also have access to both the requested URL and query string in one line.

Jim

heartless09

12:14 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



Thanks for your help.

I have changed the second rewrite to:

RewriteCond %{THE_REQUEST} ^.*alfabatic\.php$ [NC]
RewriteCond %{QUERY_STRING} ^alfat\=([a-zA-Z0-9]+)$ [NC]
RewriteRule ^.*$ titels/%1/? [R=301,L]

My first rewrite rule worked but the second didn't, probably because it gives that long line with querystring and hppt stuff at the end.

Then I tried this:

RewriteCond %{THE_REQUEST} ^.*alfabatic\.php?alfat\=([a-zA-Z0-9]+)\ HTTP/1\.1$ [NC]
RewriteRule ^.*$ titels/%1/? [R=301,L]

Also didn't work. It didn't redirect to the new URL.
This one didn't work also:
RewriteCond %{THE_REQUEST} ^.*alfabatic\.php?alfat\=([a-zA-Z0-9]+)$ [NC]
RewriteRule ^.*$ titels/%1/? [R=301,L]

I also did this but it still comes in a dealoop when I request [domainname.com...] and [domainname.com...]

mrw_alfabetic.php is a copy of alfabetic.php.

RewriteEngine on
RewriteBase /testfolder

#Static to dynamic
RewriteRule ^titels/([A-Za-z0-9]+)/page_([0-9]+)\.html$ mrw_alfabatic.php?alfat=$1&start=$2 [L]
RewriteRule ^titels/([A-Za-z0-9]+)(/)?(index\.html)?$ mrw_alfabatic.php?alfat=$1 [L]

#dynamic to static
RewriteCond %{REQUEST_URI} ^.*alfabatic\.php$ [NC]
RewriteCond %{QUERY_STRING} ^alfat\=([a-zA-Z0-9]+)$ [NC]
RewriteRule ^.*$ titels/%1/? [R=301,L]

I have seen so many examples of this but I can't get the rules to work on my website.

heartless09

12:26 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



I got it working.
I renamed my alfabatic.php to alfabatic_mrw.php

Is it possible to rewrite dynamic URL to static URL without renaming the files?

jdMorgan

3:45 pm on Mar 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code you tried is closest to the correct usage of THE_REQUEST, since it contains both the URL and the query:

RewriteCond %{THE_REQUEST} ^.*alfabatic\.php?alfat\=([a-zA-Z0-9]+)\ HTTP/1\.1$ [NC]
RewriteRule ^.*$ titels/%1/? [R=301,L]

But remember that the "?" character has special meaning in regular expressions, and must be escaped to be recognized as a literal. You also might have the optional start parameter in the incoming request, so that must be accounted for as well:

RewriteCond %{THE_REQUEST} alfabatic\.php\?alfat=([a-z0-9]+)[^\ ]*\ HTTP/ [NC]
RewriteRule alphabatic\.php$ titels/%1/? [R=301,L]

In addition [a-zA-Z] is not needed if the [NC] flag (no case) is used, and the rule processing can be sped up slightly by including the (redundant) URL-path in the RewriteRule as well as in the RewriteCond (This is because RewriteConds are not processed unless the pattern in the RewriteRule matches, and there's no need to process that RewriteCond unless the requested URL-path is 'alfabatic.php.) The HTTP revision could vary from HTTP/1.0 to HTTP/<some high number> over time, so don't bother to check the actual number.

Jim

heartless09

5:37 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



Thank you very much Jim. It works great..

Small question:
RewriteCond %{THE_REQUEST} alfabatic\.php\?alfat=([a-z0-9]+)[^\ ]*\ HTTP/ [NC]
RewriteRule alphabatic\.php$ titels/%1/? [R=301,L]

I thought that every condition in a rule has to start with "^" but in the rules that you gave me they don't have a start tag.
Is that allowed when RewriteCond is used?
Does it make a difference not to using the start tag?

Anyways thank you for your help.