Jump to content

Easier way of doing this?


ukweb

Recommended Posts

Hi

 

I've redesigned the site in work, and am having problems with our archives. Basically the redirect works nicely, but there are two problems with it;

 

1. The redirect appends the query string to the redirect, ie news_archive.php?month=1 redirects to /archives/1?1 instead of just /archives/1

 

2. I have a condition and a rule for every month, is there not a way of putting it into 1 line?

 

Here's the code. Thanks in advance!

RewriteCond %{QUERY_STRING} ^month=1$
RewriteRule ^news_archive.php$ http://www.uspcreative.com/archives/1 [R=301,L]

RewriteCond %{QUERY_STRING} ^month=2$
RewriteRule ^news_archive.php$ http://www.uspcreative.com/archives/2 [R=301,L]

RewriteCond %{QUERY_STRING} ^month=3$
RewriteRule ^news_archive.php$ http://www.uspcreative.com/archives/3 [R=301,L]

RewriteCond %{QUERY_STRING} ^month=4$
RewriteRule ^news_archive.php$ http://www.uspcreative.com/archives/4 [R=301,L]

 

Link to comment
https://forums.phpfreaks.com/topic/192368-easier-way-of-doing-this/
Share on other sites

Everywhere I look seems to indicate that simply placing a question mark at the end of your target address will nullify the QUERY_STRING, having said that I've tested it and it doesn't seem to work on my system. I just thought I'd mention it just in-case you have more success with it.

 

RewriteCond %{QUERY_STRING} ^month=1$
RewriteRule ^news_archive.php$ http://www.uspcreative.com/archives/1? [R=301,L]

 

Alternatively how about the more succinct...

 

RewriteCond %{QUERY_STRING} ^month=([0-9]{1,2})$
RewriteRule ^news_archive.php$ http://www.uspcreative.com/archives/%1? [R=301,L]

 

This does work for me. It will obviously also redirect month=13 etc. but that will obviously just throw a 404 file not found error. If this is a problem for you there's probably a workaround.

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.