Link to home
Start Free TrialLog in
Avatar of Neil_Bradley
Neil_BradleyFlag for New Zealand

asked on

RewrteRule using htacess

Hi EE,
I'm trying to clean up a site that has recently been re launched by redirecting all old URL's to their corresponding new ones.
Trouble is we have a lot of URL's that end like this:
page.php?cid=4770
page.php?cid=4772
page.php?cid=4773
and I mean a lot!
RewriteRule ^page.php?cid=4773 http://www.mydomain.com/
resolves to http://www.mydomain.com/?cid=4773 which is not what I want
Is there are way to deal with all of these page.php?cid=4773 so that they all neatly resolve to http://www.mydomain.com/
Cheers,
N
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

The query string part is not generally looked at in Rewrite Rules or Conditions, It is an extra part, see https://wiki.apache.org/httpd/RewriteQueryString So it's not at all a hard problem to treat a redirection without looking at the query string part.

If you want the query string to stay as is, then simply have RewriteCond only acting on %{REQUEST_URI}, that doesn't contain these query parameter part anyway.

Perhaps activate RewriteLog and see what the rewrite engine really sees and does: https://wiki.apache.org/httpd/RewriteLog

Bye, Olaf.
Avatar of Neil_Bradley

ASKER

Just to reiterate I need
http://www.mydomain.com/page.php?cid=4773 to rewrite to http://www.mydomain.com
Cheers,
N
Well, wouldn't that just be done by this?
RewriteRule ^page.php http://www.mydomain.com/

Bye, Olaf
ASKER CERTIFIED SOLUTION
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am stunned it was that simple. Thanks so much Olaf. I've been slaving over that for a couple of hours!
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{QUERY_STRING} cid
    RewriteRule (.*) https://your.domain.com?    [L,R]
</IfModule>

Open in new window

:) sorry hadn't refreshed before I posted!  You might benefit from the RewriteCond to only rewrite those with that query variable
No big deal. The RewriteQueryString page I pointed to has that under RewriteRule ^/page /page? removing the query string. It was unclear you wanted to remove it. Whatever new script running at the root URL could still process the parameter.

In any case it wouldn't hurt to keep the query string, if the parameter is not processed, it's not processed. The main redirection is just sending any page.php call to root, with or without the questionmark at the end clearing it out. You could log it, besides it could be good to answer with a permanent redirect via 301 status code.

RewriteRule ^page.php https://your.domain.com?    [L,R=301]

R only causes a temporary 302 redirect.

Bye, Olaf.
@Olaf, certainly the best solution here and 100% on the 301 redirect.  Thanks for pointing that out.
Again no bid deal, but thanks, Rob.

100% best depends on whether it should be permanent, but temporary redirects are seldom. Temporary redirects could help to fix a problem while current visitors are rerouted and then go back whether to a new php page or not, with cid again.

It's obviously impossible to turn back a request of the root domain to individual requests with some cid later with yet another rewrite rule.

Bye, Olaf.