.htaccess redirectmatch 301 - .htaccess

I want to redirect as follows using .htaccess redirectmatch 301 rule.
RedirectMatch 301 ^/user.php?id="user_id" /user/
I don't want query string to be appended in redirected URL.
Is it possible?
Thanks in advance.

QueryString is not part of match in RedirectMatch directive.
To match against querystrings you need to use mod_rewrite :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=userId$ [NC]
RewriteRule ^user\.php$ /user? [L,R=301]
Empty question mark at the end of the target url is important, as it discards the orignal querystrings from url.

Related

301 redirect from multiple url parameters

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

.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]

.htaccess 301 rewrites with question marks

I'm using something similar to the following:
RewriteEngine on
Redirect 301 / http://newdomain.co.uk/link/
Redirect 301 /showcase.asp?showcaseid=1 http://newdomain.co.uk/track1
Redirect 301 /showcase.asp?showcaseid=2 http://newdomain.co.uk/track2
Redirect 301 /showcase.asp?showcaseid=3 http://newdomain.co.uk/track3
Redirect 301 /showcase http://newdomain.co.uk/link/tracks
With that in mind any URL other than the mentione would go to http://newdomain.co.uk/link
Which is fine however any of the other URL's that use a "?" go to say http://newdomain.co.uk/link/showcaseid=1 for /showcase.asp?showcaseid=1
Also with the
Redirect 301 /showcase http://newdomain.co.uk/link/tracks
can I just write as follows:
Redirect 301 /showcase tracks
You can't match against the query string (everything after the ?) in a Redirect directive. You have to use mod_rewrite's %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^showcaseid=([0-9])
RewriteRule ^showcase\.asp$ http://newdomain.co.uk/track%1 [L,R=301]
RewriteRule ^showcase$ http://newdomain.co.uk/link/tracks [L,R=301]
RewriteRUle ^$ http://newdomain.co.uk/link/ [L,R=301]
Note that the order is important. You generally want the more general matching rules to be at the end.

RedirectMatch not working in 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]

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