Jump to content

Redirect not working


siwelis

Recommended Posts

Hello Ladies and Gents, I'm getting back into coding after a year lapse. Got my first big project coming up soon too, but in the meantime, I'm working on my personal site and am LOST on a piece of code (Mod_Rewrite) that I gave up on years ago... Or did I give up?! No! I'm finally seeking help!!!

 

#below masks URL successfully to html
RewriteRule ^a/(.*)/(.*)\.html$ /a/$1/?req=read&article_title=$2 [L]


#however, when I try to force that old URL to go to the HTML version, absolutely nothing happens
RewriteRule ^a/(.*)/?req=read&article_title=(.*)$ /a/$1/$2.html [R]

Does anyone see what I did wrong in the "Redirect" code below?

 

Sweet lord almighty! I've tried to figure this out on my own numerous times over the years. Pray ye have the answer?  :sweat:

 

Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/296651-redirect-not-working/
Share on other sites

RewriteRule cannot capture the values in a querystring. You need to use a RewriteCond on the %{QUERY_STRING} server variable to capture the article_title query string value

#below masks URL successfully to html
#NOTE: the &r=0 query string param this is to prevent an infinite redirect loop
RewriteRule ^a/(.*)/(.*)\.html$ /a/$1/?req=read&article_title=$2&r=0 [L]

# redirect old url format to new .html format
RewriteCond %{QUERY_STRING} ^req=read&article_title=([^&]*)$ [NC]
RewriteRule ^a/(.*)/$ /a/$1/%1.html? [R=301,L]

Awesome! That was perfect. I was going to figure out the 301 later, but you even had that in there. Thank you!!!

 

My only change was for my memory later changing the "&r=0" to "&null=0" so I'll remember when I look at it later.

 

Thanks again!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.