siwelis Posted June 5, 2015 Share Posted June 5, 2015 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? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/296651-redirect-not-working/ Share on other sites More sharing options...
Solution Ch0cu3r Posted June 5, 2015 Solution Share Posted June 5, 2015 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] 1 Quote Link to comment https://forums.phpfreaks.com/topic/296651-redirect-not-working/#findComment-1513231 Share on other sites More sharing options...
siwelis Posted June 7, 2015 Author Share Posted June 7, 2015 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/296651-redirect-not-working/#findComment-1513385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.