htaccess 301 redirect not working with my site - .htaccess

I have rewrite URL via htaccess now I want to redirect that URL to another URL.
Now I want to redirect http://www.domain.com/tag/example-page URL to http://www.domain.com/tag/example
redirect 301 /tag/example-page http://www.domain.com/tag/example
Please help me regarding this issue.
Below is my rewrite rule, which I am using for short URL
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Keep this rule as your very first rule in root .htaccess:
RedirectMatch 301 ^/tag/example-page/?$ /tag/example

Related

How to stop RewriteRule overruling Redirect 301 in htaccess

I would like to make some massive redirects in .htaccess because of a migration. I'm using both RewriteRule and Redirect 301. But the RewriteRule keeps overwriting the Redirect 301 rules, despite of the order.
For example:
I would like to redirect /retraites-nl-cat-595.html to https://www.vihara.nl/meditatieretraites/
But all the other url's that contains 595 to https://www.vihara.nl/tag/vipassana/
I have this in .htaccess:
RewriteEngine on
RewriteRule ^$ https://www.vihara.nl [R=301]
Redirect 301 /retraites-nl-cat-595.html https://www.vihara.nl/meditatieretraites/
RewriteRule ^(.*)595(.*)$ https://www.vihara.nl/tag/vipassana/ [L,R=301]
But this is not working because the RewriteRule is overruling the Redirect 301 rule. The same happens when I switch the order of the last 2 lines.
Is there a way to change this? So the Redirect 301 will be overruling the Rewrite rule, instead of the other way around?

Redirect 301 specific pages and the rest to just one page

First I need to redirect these pages to another page in a different domain
Redirect 301 /example1 http://newdomain.com/test1
Redirect 301 /example2 http://newdomain.com/random1
Note the pages are not the same in the new domain (e.g., /example1 to /test1)
After that, I need redirect the rest of the pages to newdomain.com
E.g., Redirect 301 (everything else) to http://newdomain.com
Try below rule, using mod rewrite I am assuming you have mod rewrite enabled.
RewriteEngine On
RewriteRule ^example1$ http://newdomain.com/test1 [R=301,L]
RewriteRule ^example2$ http://newdomain.com/random1 [R=301,L]
RewriteCond %{REQUEST_URI} !^(example1|example2)
RewriteRule ^ http://newdomain.com [R=301,L]
If you want to use mod-alias , you can use these redirects :
RedirectMatch 301 ^/example1/?$ http://example.com/test1
RedirectMatch 301 ^/example2/?$ http://example.com/random1
#redirect everything else to the homepage of example.com
RedirectMatch ^.+$ http://example.com/
Clear your browser cache before testing these redirects.
Try this in your .htaccess
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

htaccess 301 redirect with question mark

I have list of URL (600 approximately) that needs to be redirected, few URL are:
Redirect 301 /assets?/elfinder1?/elfinder.html http://www.example.com/swiftkanban/
Redirect 301 /backup?/elfinder?/elfinder.html http://www.example.com/swiftkanban/
Redirect 301 /blog?/xmlrpc.php http://www.example.com/blog/
Redirect 301 /blog.feed?type=rss http://www.example.com/blog/
Redirect 301 /m?/ http://www.example.com/swiftkanban/
Redirect 301 /m/ http://www.example.com/swiftkanban/
Redirect 301 /mobile?/ http://www.example.com/swiftkanban/
Redirect 301 /mobile/ http://www.example.com/swiftkanban/
Redirect 301 /SgjRY?/blog?/16-david-blog?/63-who-owns-kanban.html http://www.example.com/blog/who-owns-kanban/
Redirect 301 /support?/about.html http://www.example.com/about-us/
Redirect 301 /support?/about_us http://www.example.com/about-us/
Redirect 301 /support?/aboutus http://www.example.com/about-us/
Redirect 301 /support?/company http://www.example.com/about-us/the-team/
Redirect 301 /support?/contact.html http://www.example.com/contact-us/
Redirect 301 /support?/contact_us http://www.example.com/contact-us/
Redirect 301 /support?/contactus http://www.example.com/contact-us/
The problem is, I cannot do wildcard redirect, and this redirect rule does not work, I tried escaping the question mark using \ but this does not seem to work, can someone point me to the right direction?
Redirect directive cannot match query string. You need to use mod_rewrite based rules and use a RewriteCond directive as example below:
RewriteEngine On
RewriteCond %{THE_REQUEST} /blog\?/xmlrpc\.php [NC]
RewriteRule ^ http://www.example.com/blog/? [L,NE,R=301]
RewriteCond %{THE_REQUEST} /blog\.feed\?type=rss [NC]
RewriteRule ^ http://www.example.com/blog/? [L,NE,R=301]
Trailing ? after /blog/ is for removing pre-existing query string. If you want query string after redirect then remove ? from target URL.
References:
Apache mod_rewrite Introduction
.htaccess tips and tricks

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

301 Redirect entire site to new site using .htaccess file

Ive got a site siteONE.co.uk that i want to 301 permanent redirect all the pages to another site, siteTWO.co.uk
For this im currently using the this .htaccess file below, but im have to type out a new line for each page i want to rediect even though they are all going to the homepage of siteTWO.co.uk. They are also redirecting incorrectly, instead of siteONE.co.uk/aberdeen going to siteTWO.co.uk its going to siteTWO.co.ukaberdeen (note there is not / between the .co.uk and aberdeen)
Any ideas were im going wrong with this ?
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(http://siteONE\.co\.uk)(:80)? [NC]
RewriteRule ^(.*) http://www.siteONE.co.uk/$1 [R=301,L]
order deny,allow
RewriteEngine on
RewriteRule ^/?$ http://siteTWO.co.uk/ [R=301,L]
redirect 301 / http://siteTWO.co.uk
redirect 301 /aberdeen http://siteTWO.co.uk
redirect 301 /bath http://siteTWO.co.uk
redirect 301 /belfast http://siteTWO.co.uk
redirect 301 /birmingham http://siteTWO.co.uk
redirect 301 /cambridge http://siteTWO.co.uk
redirect 301 /canterbury http://siteTWO.co.uk
redirect 301 /chester http://siteTWO.co.uk
redirect 301 /york http://siteTWO.co.uk
You can get rid of all the code in your .htaccess and use this rule instead:
RewriteEngine on
RewriteRule ^ http://siteTWO.co.uk/? [R=301,L,NE]
Make sure to clear your browser cache before testing this.

Resources