Redirect query (?page=) to different URL? - apache

I need to redirect anything with ?page= or ?page="" back to the homepage.
I tried RewriteRule ^/\?page=(.*)$ / [R=301,L] but it doesn't work.
Example:
http://example.com/?page= or http://example.com/?page=test or
http://example.com/?page="test"

RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /404.php? [R,L]
Did the trick.
The RewriteCond part catches any query (.) and then the RewriteRule redirects it to a 404 page.

Related

Apache .htaccess - 301 redirecting home page

I'm trying to redirect website A to website B. All of the sub-pages are redirecting as intended, but the home page is not. Here is a snippet of my .htaccess:
# HOMEPAGE (This one isn't working)
RewriteRule ^/$ http://www.drinkinggamezone.com [L,R=301]
# SUBPAGE (Works)
RewriteRule ^1990s-rock-power-hour/?$ http://drinkinggamezone.com/power-hours/1990s-rock? [L,R=301]
You have an unnecessary / in the first part of your rule, it should be :
RewriteRule ^$ http://www.drinkinggamezone.com [L,R=301]
Regarding index.php, just do the same (again, without /, obviously) :
RewriteRule ^index.php$ http://www.drinkinggamezone.com [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]
Hope it will work.

Redirection is not working with mod_rewrite in htaccess

I need to redirect few URIs having query string like:
/pages/foo.bar?pageId=123456 to http://some.site/spam/egg/
/pages/foo.bar?pageId=45678 to http://another.site/spaming/egging/
I have this in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=123456$
RewriteRule ^.*$ http://some.site/spam/egg/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/ [R=301,L]
But its not working, showing 404. What am i doing wrong?
You need to move these 2 rules i.e. before all other rules just below RewriteEngine On line as other rules might be overriding this.
(Based on your comments) Your culprit rule is this rule:
RewriteRule . index.php [L]
Which is actually rewriting every request to index.php and changing value of REQUEST_URI variable to /index.php thus causing this condition to fail:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
From your example, you get redirected to
http://some.site/spam/egg/?pageId=123456
http://another.site/spaming/egging/?pageId=45678
You can use your browser developer tools to see the redirection (in the Network tab).
Maybe the query strings in the redirected URL lead to a 404? You can add a ? at the end of your redirection to clear the query string:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/? [R=301,L]

Strange behavior with URL rewriting

I'm trying to rewrite some url's. So I have page which is userAction.php and I rewrite it to /login/ like this
RewriteRule ^login/$ /userAction.php [L]
now on that page login I have two forms - Login and Register. When user make registration and he finish with it the page is still userAction.php but with message: userAction.php?action=joined. I want to rewrite this to login?action=joined. Here is what I'm tried but it it redirecting me to 404 error
RewriteRule ^login?action=joined/$ /fun-second/userAction.php?action=joined [L]
So this is the whole .htaccess
RewriteRule ^login/$ /userAction.php [L]
RewriteRule ^login/?action=joined/$ /userAction.php?action=joined [L]
And this is the redirect when registration is successfully
header('Location: login/?action=joined/');
exit;
Note: With normal URL's is working and there is no problems.
You'd use %{QUERY_STRING} variable to match it.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(action=joined)$ [NC]
RewriteRule ^login/$ /userAction.php?%1 [NC,L]
RewriteRule ^login/$ /userAction.php [NC,L]
The order of rules is also important. Alternatively, you can try the following approach:
RewriteEngine On
RewriteRule ^login/$ /userAction.php [QSA,NC,L]
Note the QSA flag above.

htaccess redirect urls with parameters to specific directory

i want to redirect specific urls from
http://www.domain.com/com/page.asp?id=99
to
https://www.domain.com/de/directory/
and all urls from
http://www.domain.com/com/page.asp
to
https://www.domain.com/de/
ive tried some redirects but i cant get it done right.
Please help me!
RewriteCond %{query_string} id=5
RewriteRule (.*) https://www.domain.com/de/directory/? [R=301,L]
this works, but the biggest problem is all urls in the cms https://www.domain.com/cms are redirected too if they contain an id
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=99$
RewriteRule ^page\.asp$ /de/directory/ [L,R]
RewriteRule ^page\.asp$ /de/ [L,R]

htaccess mobile redirectioon to subdomain not working properly

I'm using following htaccess code to redirect mobile users
# Mobile Redirection
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^$ http://m.mydomain.com [R,L]
Problem is it works only ehen user type http://mydomain.com/
If I type http://mydomain.com/anyurl it will not redirect to subdomain. Pls help.
You are matching the root url with the regex ^$.
Try ^.*$, and if you want to redirect to the relevant mobile page, the line should be RewriteRule ^(.*)$ http://m.mydomain.com/$1 [R,L] ($1 is the matched pattern in brackets).
Change your rewrite rule to the following:
RewriteRule ^.*$ http://m.mydomain.com [R,L]