I want to redirect this type url to my home page
http://www.mywebsite.com/index.php?showuser=
to
http://www.mywebsite.com/
Can I do this with .htaccess? How to do it. I tried this but works nothing.
redirectMatch 301 /index.php?showuser= http://www.mywebsite.com
And
RedirectMatch 301 /index.php\?showuser\= http://www.mywebsite.com
You can't match against the query string in the RedirectMatch regex. You need to use mod_rewrite and the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^showuser=
RewriteRule ^index\.php$ /? [L,R=301]
Neither Redirect or RedirectMatch can match the query string. The ? in RedirectMatch actually means "the previous character 0 or 1 times", and would be matched against "index.php". Please note that you are not using the dot correctly too.
You can solve this by using mod_rewrite. Make sure that it is enabled, and that you allow override of FileInfo. Then use the following directives:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^showuser=$
RewriteRule ^index\.php$ / [R,L]
Change [R] to [R=301] if you wish to make the redirect permanent after testing everything works as expected.
Related
I'm trying to redirect any of these formats
mydomain.com/?main_page=products_new
mydomain.com/?main_page=products_new&page=2
mydomain.com/?main_page=products_new&disp_order=2
to this
mydomain.com/new-products
I have this working for only the first parameter. How can I ignore the remaining ones and match on anything that contains ?main_page-products_new ?
RewriteCond %{QUERY_STRING} ^main_page=products_new [NC]
RewriteRule ^(.*) /new-products? [L,R=301]
Apache supports RedirectMatch
RedirectMatch 301 ^main_page=products_new.*$ /new-products?$1
I try to redirect a ddos attacks. I use redirectmatch conditions, but somehow it is not working in my htacces. the code is
RedirectMatch 301 \d*\?_uw=\d* http://www.someurl.com
or
RewriteRule ^\d*\?_uw=\d* http://www.someurl.com [L,R=301]
None of above is working. I would like to match following urls where numbers are always changing 197831051715412?_uw=9351823359
You can use:
RewriteEngine on
RewriteCond %{QUERY_STRING} _uw=\d*
RewriteRule ^\d*$ http://www.someurl.com/? [L,R=301]
With this other last line:
RewriteRule ^ http://www.someurl.com/? [L,R=301]
You don't test file name (if they also use letters) only _uw=
i would like to make a 301 redirect and from an old website using the exact exact url with no extra parameters.
example:
/en-direct.php?page=7
to go to:
http://www.example.org/news/
and page:
/en-direct.php?page=8
to go to:
http://www.example.org/awesome-but-totally-different-page/
i used:
redirectMatch 301 ^/en-direct.php$ http://www.example.org/different-page/
redirectMatch 301 ^/en-direct.php?page=7$ http://www.example.org/news/
redirectMatch 301 ^/en-direct.php?page=8$ http://www.example.org/awesome-but-totally-different-page/
however: i get http://www.example.org/different-page/ every time with all the parameters from the redirect page (example - http://www.example.org/different-page?page=7 )
any help will be much appreciated.
In order to match against the query string, you need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=7($|&)
RewriteRule ^en-direct\.php$ http://www.example.org/news/ [L,R=301]
RewriteCond %{QUERY_STRING} ^page=8($|&)
RewriteRule ^en-direct\.php$ http://www.example.org/awesome-but-totally-different-page/ [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^en-direct\.php$ http://www.example.org/different-page/ [L,R=301]
I have the following for a page:
RewriteRule ^es/aprende-([^/]+)-online$ learn-language.php?learnLang=$1&lang=es [L]
My new page:
RewriteRule ^es/aprende-ingles-online$ learn-english-online.php?lang=es [L]
Now I want to do a 301 redirect on this page:
RedirectMatch 301 /es/aprende-inglés-online http://www.example.com/es/aprende-ingles-online
But when I go to the page the variables get carried on and appended to the url ?
learnLang=inglés&lang=es
I dont want this get variables to be added on, what do I do?
You need to keep redirect rule before your earlier internal rewrite rule and use mod_rewrite rules only:
RewriteEngine On
RewriteBase /
RewriteRule ^es/aprende-inglés-online/? /es/aprende-ingles-online [L,R=301]
RewriteRule ^es/aprende-ingles-online$ learn-english-online.php?lang=es [L,QSA]
RewriteRule ^es/aprende-([^/]+)-online$ learn-language.php?learnLang=$1&lang=es [L,QSA]
I want to 301 redirect from
domain.com/?test-article.php
to
domain.com/full-article/test-article.php
Here is my code what i am trying to use and its not working.
redirect 301 /?test-article.php http://domain.com/full-article/test-article.php
or
rewriteRule /?test-article.php http://domain.com/full-article/test-article.php [R=301,L]
if i use without '?' then it re-directs from
/test-article.php
to
http://domain.com/full-article/test-article.php
So something has to do with '?'
You can't match against the query string using a pattern of a Redirect or RewriteRule. You need to use the %{QUERY_STRING} variable and mod_rewrite:
RewriteCond %{QUERY_STRING} ^test-article$
RewriteRule ^/?$ http://domain.com/full-article/test-article.php [R=301,L]