Forum Moderators: phranque
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.
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
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.
RewriteCond %{THE_REQUEST} ^.*alfabatic\.php?alfat\=([a-zA-Z0-9]+)\ HTTP/1\.1$ [NC]
RewriteRule ^.*$ titels/%1/? [R=301,L]
RewriteCond %{THE_REQUEST} alfabatic\.php\?alfat=([a-z0-9]+)[^\ ]*\ HTTP/ [NC]
RewriteRule alphabatic\.php$ titels/%1/? [R=301,L]
Jim
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.