Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite R=301 issue

mod_rewrite 301

         

viktor

2:50 pm on Oct 8, 2008 (gmt 0)

10+ Year Member



Good day, I have a trouble while redirecting to URL, this URL is an alias (site.com/?cID=aliasname) of some index.php?id=idnumber, generated by some CMS system.

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]

2. Rewrite a path ending in "<something>.html" to
"index.php?id=<something>"
The "[L]" means that if a url matches this rule, do not apply any of the the following rules to it.

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

viktor

2:55 pm on Oct 8, 2008 (gmt 0)

10+ Year Member



Sorry i mean somebody LOL. :)

jdMorgan

9:00 pm on Oct 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Steps 1 and 2 look OK, but after that, things need some work:

# 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]

Back-references are numbered %1 through %9 (for RewriteCond) and $1 through $9 (for RewriteRule) only.

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

viktor

10:18 pm on Oct 8, 2008 (gmt 0)

10+ Year Member



Hi Jim, thank you very much for your time and help, I tested the solution you gave me and now I got a different result. I received a message from my Firefox navigator wich says this:

----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

viktor

11:02 pm on Oct 8, 2008 (gmt 0)

10+ Year Member



I forgot to mention that it redirects properly to the url with the .html, but just doesn't show it, like i mentioned before.

Thanks

jdMorgan

12:36 am on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The RewriteCond requires a query string, but then the RewriteRule removes that query string. And even if the admin_mode cookie is not set and remains not set, the query string being cleared is the loop-stopper. So this rule cannot loop all by itself.

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

instead of rewriting ^$ to /index.php as in your first rule... Your choice, of course.

Jim

jdMorgan

12:40 am on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, duh! It's your second rule that interferes with this one... :o

Add another RewriteCond (to your third rule):


RewriteCond %{REQUEST_URI} !^/index\.php$

Jim

viktor

3:39 am on Oct 9, 2008 (gmt 0)

10+ Year Member



It works like a charm Jim! thank you very much for your collaboration, It redirects perfect. I have a last question, now my URL it's like http://www.example.com/opc1.html, wich is right, but the menu items are now referenced with the new URL: http://www.example.com/opc1.html?cID=opc3, and when i click on it it does nothing, hence doesn´t reference to the .html of the menu, example opc3.html.

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!

jdMorgan

12:39 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but the menu items are now referenced with the new URL: http://www.example.com/opc1.html?cID=opc3

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

viktor

1:25 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



The site is generated by a CMS, and it assigns an alias for each new page created, and create in this format... www.example.com/?cID=opc1,..?cID=opcN
So, for example when i am in the main site and click on opc1 it is ok it rewrite to www.example.com/opc1.html, but once i get there the menus are still append the query string to the URL, now www.example.com/opc1.html?cID=opc2, and then when i click again it does nothing because the rewrite rules (i think) are for the other format.. the www.example.com?cID=opc1,...?cID=opcN not for the new www.example.com/opcN.html?cID=opcM..

Thanks!