Help with R=301 Rewrite full path instead single .html file

Discuss practical ways rearrange URLs using mod_rewrite.

Help with R=301 Rewrite full path instead single .html file

Postby victor_r » Wed Oct 08, 2008 10:12 am

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:

Code: Select all
RewriteEngine On
#It supposes to rewrite all requests with no path specified to index.php, "[QSA]" says to append the query string.
RewriteRule ^/$ /index.php [QSA]

#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 thefollowing rules to it. 
RewriteRule ^/([A-Za-z0-9_-]+)\.html$ /index.php?id=$1 [L]

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

#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][^&]+$

#Take the match from the condition above and match the part of it between the "=" and the end.
RewriteCond %0 [^=]+$

#Finally, 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. The "R=301" means make this an external redirect (send a redirect header to the browser so that it changes the url pointed to) and indicate that
the content has permanently moved.
RewriteRule .* %0.html? [R=301,L]


That's the code I have in my .htaccess file, it is located on my /public_html folder where the content of my site. The problem is that when I click in some of the menu options the 404 not found appears.
The redirect is pointed to something like:

http://www.example.com/home/example/pub ... wpage.html

instead of:

http://www.example.com/newpage.html

It gets the full path of the site, I don't know why, hence don't know how to fix it 'cause I'm new in mod_rewrite.

Thanks in advance!

Víctor
victor_r
 
Posts: 4
Joined: Wed Oct 08, 2008 9:59 am
Location: México, MX

Postby victor_r » Wed Oct 08, 2008 12:13 pm

Ok, now I got it to redirect correctly. Now it's pointed to something like this www.example.com/page.html. I just added a '/' before the '%0.html?' on the last line,

Now it looks like this:

Code: Select all
RewriteRule .* /%0.html? [R=301,L]


But it still gets a error of 404 not found.
victor_r
 
Posts: 4
Joined: Wed Oct 08, 2008 9:59 am
Location: México, MX

Postby victor_r » Wed Oct 08, 2008 2:28 pm

Ok, searching in the web a little bit and asking to people who knows about this I cut and edited my code a little like this:

Code: Select all
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]


After I proved in my navigator it sends me the message below:

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. I think I could be closer to the solution by anyway I'm a newbie and If anybody know the answer please let me know.

Thanks in advance and good day!

Víktor
victor_r
 
Posts: 4
Joined: Wed Oct 08, 2008 9:59 am
Location: México, MX

Postby richardk » Thu Oct 09, 2008 11:32 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^$ index.php [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_COOKIE} !^(.*;\ )?(COOKIE_ADMIN_MODE=true)(;\ .*)?$
RewriteCond %{QUERY_STRING} ^(.*&)?c?id=([^0-9&]+)(&.*)?$ [NC]
RewriteRule .* http://www.example.com/%2.html? [R=301,L]

RewriteRule ^(.+)\.html$ /index.php?id=$1 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby victor_r » Thu Oct 09, 2008 11:58 am

Hi Richard, thank you for your time and answer, Yesterday in another forums i received a lot of help and the conclussion was this code:

Code: Select all
RewriteEngine On
DirectoryIndex index.php
RewriteRule ^(.*)\.html$ index.php?id=$1 [L,NC]
RewriteCond %{HTTP_COOKIE} !(COOKIE_ADMIN_MODE=true)
RewriteCond %{QUERY_STRING} ^(id|cID)=([^0-9&]+)$
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule .* http://www.example.com/%2.html? [R=301,L]


And works very good!

Anyway, thanks for your help, i appreciated.

Víctor
victor_r
 
Posts: 4
Joined: Wed Oct 08, 2008 9:59 am
Location: México, MX


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 6 guests

cron