Forum Moderators: phranque
My rules are as follows:
RewriteEngine On
1. Rewrite all requests with no path specified to index.php, "[QSA]" says to append the query string.
RewriteRule ^/$ /index.php [QSA]
RewriteRule ^/([A-Za-z0-9_-]+)\.html$ /index.php?id=$1 [L]
3. If the current request does not have a cookie called "COOKIE_ADMIN_MODE" with a value of "true"
RewriteCond %{HTTP_COOKIE} !(COOKIE_ADMIN_MODE=true)
4. If the current request has a query string starting with "id=" or "cID=" and the part following that is not a number and not empty.
RewriteCond %{QUERY_STRING} ^(id¦cID)=[^0-9][^&]+$
5. Take the match from the condition above and match the part of it between the "=" and the end.
RewriteCond %0 [^=]+$
6. Rewrite any path (but this rule is only applied if the above conditions are true) to the match from the rule above with ".html" appended. The "?" clears the query string.
RewriteRule .* %0.html? [R=301,L]
The problem is that the redirect returns a full path URL, something like:
http://www.mysite.com/home/syte/public_html/section1.html
in consequence sends a 404 error.
instead of just return something like:
http://www.mysite.com/section1.html
I used the [PT] flag but sends me another error from apache.
I hope anybody can help,
Thanks in advance! :)
Viktor
# If not admin mode
RewriteCond %{HTTP_COOKIE} !(COOKIE_ADMIN_MODE=true)
# Capture id/cID to %2
RewriteCond %{QUERY_STRING} ^(id¦cID)=([^0-9&]+)$
# Delete the following line -- It is not needed
# RewriteCond %0 [^=]+$
# Clear query and redirect to www.example.com/<id/cID value>.html
RewriteRule / http://www.example.com/%2.html? [R=301,L]
Specifying the full URL substitution in your redirect rules is a good idea, and will probably help in this case. If not, check your current DocumentRoot definition. If correct, then see the RewriteBase directive.
I presume your code is intended for use in a server config file, and not in .htaccess. If this code *is* for use in .htaccess, then remove the leading slash from your URL-path patterns in steps 1 and 2. and change the RewriteRule pattern in the last rule (in this post) back to ".*"
Note that [a-z0-9] used with an [NC] flag is equivalent to [a-zA-Z0-9], but is a full 33% faster to process.
Jim
----The page is not redirecting properly----
Firefox has detected that the server is forwarding the request to this address in a way that will never end.
This problem is sometimes caused by disabling or reject the receipt of cookies.
This happens in both Explorer and Firefox.
But I have enabled the cookies for both, Iexplorer and Firefox. Looks like the redirect falls in an infinity loop.
The code is these:
RewriteEngine On
RewriteRule ^$ index.php [QSA]
RewriteRule ^(.*)\.html$ index.php?id=$1 [L,NC]
RewriteCond %{HTTP_COOKIE} !(COOKIE_ADMIN_MODE=true)
RewriteCond %{QUERY_STRING} ^(id¦cID)=([^0-9&]+)$
#RewriteCond %2 [^=]+$
RewriteRule .* http://www.example.com/%2.html? [R=301,L]
Thanks in advance and good day!
Víktor
If you haven't already got it, install "Live HTTP Headers" in your FF browser, and then "watch" the redirect loop. That should point you in the right direction, because I suspect that some other rule (or a mod_alias directive) is conflicting with or countermanding this rule and the two of them affect each other, causing a loop. Live Headers will show this happening.
BTW, it would be more usual to use the directive
DirectoryIndex index.php
Jim
Could you be so kind to tell me what's missing in the script?
I think i have to edit this line :
RewriteRule ^(.*)\.html$ index.php?id=$1 [L,NC]
but i'm not sure how the rule should be.
Thanks!
I don't know how your site is *supposed* to work, so this doesn't mean anything to me. What URLs are the menu items supposed to referenced with?
Don't think about coding, think about design... Think about what URLs you want to rewrite, what URLs you do not want to rewrite, and how to tell them apart using only the URL requested by the browser, the query string appended to those URLs, and the HTTP request headers that the browser sends with those URLs.
Only after having a solid definition of the requirements can coding be done successfully.
Jim
Thanks!