RedirectMatch not working in htaccess - .htaccess

Iam trying to to redirect my page from
from http://domain.com/article.php?id=23232
to http://domain.com/article/23232
am using this syntax in .htaccess
RedirectMatch 301 article.php?id=(.*)$ http://domain/article/$1
so please what is the error in this?

You cant match query string using RedirectMatch. Use mod_rewrite rules instead:
RewriteCond %{QUERY_STRING} ^id=([^&]*)
RewriteRule ^article\.php$ http://domain.com/article/%1? [L,R=301,NC]

Related

Replace word within url and redirect

I am trying to replace word with another word in url and redirect but its not wokring due to query string.
Here is eg. Here is the url and i want to redirect it
https://www.custesitetest.com/test_calculator.php?tf=to&loc=staines
to
https://www.custesitetest.com/autoqoute?tf=to&loc=staines
RedirectMatch ^/test_calculator.php/(.*)$ https://www.custesitetest.com/autoquote/$1
You can use this rule in your root .htaccess:
RedirectMatch 301 ^/test_calculator.php?$ /autoquote
Try it like this with mod_rewrite,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^test_calculator.php
RewriteCond %{QUERY_STRING} ^(.+)
RewriteRule ^ autoquote?%1 [R=301,L]

.htaccess; 301 redirect not working

I tried the following code in .htaccess to 301 redirect www.example.com/?content=file.php&id=16 www.example.com/file/This-is-the-title/16
RewriteEngine on
Redirect 301 /?content=file.php&id=16 /file/This-is-the-title/16
But it's not redirecting. The URL remains as it is.
What am I doing wrong?
P.S. I'm not asking for rewrite or so. I need a 301 redirect.
The Redirect directive doesn't match query strings. Use this instead:
RewriteEngine on
RewriteCond %{QUERY_STRING} =content=file.php&id=16
RewriteRule ^$ /file/This-is-the-title/16? [R=301,L]

How redirect urls with special url?

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.

Redirect query url to new page

I have this url
www.example.com/nothing_here?registration=disabled
and I want to redirect it to
www.example.com/errors/418.php
I cannot get rid of the nothing_here part of the url. How to do this?
Thanks
In the htaccess file in your document root, add:
RedirectMatch 301 ^/nothing_here$ /errors/418.php?
or using mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^registration=disabled$ [NC]
RewriteRule ^nothing_here$ /errors/418.php [L,R=301]

redirectMatch 301 regex is not working

I want to redirect http://site.com/home?page=123 http://site.com/home
but the following rule doesnt work
redirectMatch 301 ^/home/\?(.*)$ http://www.site.com/
Any help would be appreciated. Thanks
Unfortunately RedirectMatch directive does not work with query string -- only with path part of the URL. You have to use mod_rewrite for that:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =page=123
RewriteRule ^home$ http://www.site.com/? [R=301,L]
Place it in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
It will ONLY redirect request for /home?page=123. All other requests (e.g. /home?page=123&extra=hello) will be ignored.

Resources