Jump to content

Redirect not working


Go to solution Solved by Ch0cu3r,

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

  • Solution

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]
  • Like 1

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.