I am writing a rewrite rule to redirect all urls from mysite.co.uk/en/ to mysite.co.uk/. I have the following I have written. Just would like someone to confirm it is correct for me and suggest possible improvements if any.
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co\.uk$
RewriteRule ^en/(.*)$ http://www\.mysite\.co\.uk/$1 [NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com\.au$
RewriteRule ^en/(.*)$ http://www\.mysite\.com\.au/$1 [NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co\.nz$
RewriteRule ^en/(.*)$ http://www\.mysite\.com\.nz/$1 [NC,R=301]
RewriteEngine off
After much research turns out I should be able to do just this:
redirect 301 /en/ http://www.mywebsite.co.uk/
redirect 301 /en/ http://www.mywebsite.com.au/
redirect 301 /en/ http://www.mywebsite.co.nz/
This we redirect any urls that are going to www.mywebsite.co.uk/en/ or mywebsite.co.uk/en/ to www.mywebsite.co.uk/
Use below code
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Please REPLACE domain.com and www.newdomain.com with your actual domain name.
Related
I can not find a working solution for me.
Here my settings:
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect http to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The each parts work very well but not together.
The part for https redirection always redirect to https://domain.tld so without www, It is ignored.
Does someone know a solution?
Thanks in advance.
Forcing HTTPS and include WWW:
RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s !^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^$ http%2://www.%1%{REQUEST_URI} [R=301,L]
Should do both in one shot, otherwise you can separate the rules and probably to it this way:
# Rewrite to WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
If the rewrite doesn't work for non-www to www then there's likely a VirtualHost 301 redirect somewhere else (probably in httpd.conf) that is set on the server... It would look similar to this:
httpd.conf
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [L,R=301]
</IfModule>
If that is set in httpd.conf it can override the rewrite in .htaccess for non-www to www.
I'm trying to move a site from a set of static html pages on one domain, to a new domain on WordPress. So I want each old page redirect to the corresponding new page on the new domain.
The structure on old domain is like so:
http://www.example.com/page.html
and the new domain has this structure:
http://www.example-new.com/category/sub-category/page/
So I tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page1.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page2.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page2/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page3.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page3/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page4.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page4/ [R=301,L]
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page5.html [NC]
RewriteRule ^(.*)$ http://www.domain-new.com/new-category/new-sub-category/page5/ [R=301,L]
And it works (kinda). The only GIANT problem is that now all pages redirect to the first new page, in this case http://www.domain-new.com/new-category/new-sub-category/page1/
Obviously I'm not an expert, but I spent hours on Google looking for an answer and trying things and can't figure how to do this, the closest I have found is about redirecting domain A to domain b removing the extension, so this is my last resource.
Any help appreciated!
This condition is the main problem:
RewriteCond %{HTTP_HOST} !^http://www.domain-old\.com/page1.html [NC]
This will always evaluate to true because variable HTTP_HOST only matches host name part of a web request not the complete URL.
You can use this single rule in your .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain-old\.com$ [NC]
RewriteRule ^([\w-]+)\.html$ http://www.domain-new.com/new-category/new-sub-category/$1/ [R=301,L,NC]
You are over-complicating this. Instead of all that processing, just use the Redirect Directive.
Redirect 301 /page1.html /new-category/new-sub-category/page1/
Redirect 301 /page2.html /new-category/new-sub-category/page2/
Redirect 301 /page3.html /new-category/new-sub-category/page3/
Redirect 301 /page4.html /new-category/new-sub-category/page4/
Trying to proper make a redirect loop for site
tryng to redirect all sites like
34www.arsenal.org.pl / 46www.arsenal.org.pl / ww32w.arsenal.org.pl / dalx.arsenal.org.pl
to arsenal.org.pl
http://www.webconfs.com/htaccess-redirect-generator.php gave me the code
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://arsenal.org.pl/$1 [R=301,L]
but it give redirect loop in Wordpress
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)arsenal.org.pl$
RewriteCond %{HTTP_HOST} ^(.*?)\.arsenal.org.pl$
RewriteRule ^(.*)$ http://arsenal.org.pl/$1 [r=301,nc]
ty Cbroe for hint :)
I'm trying to do a redirect from an old site to a new one:
OLD: blog.example.com
NEW: www.example.com/blog
To achieve the above, I got this .htaccess:
RewriteCond %{HTTP_HOST} ^blog.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^blog.example.com$
RewriteRule (.*)$ http://www.exmaple.com/blog/$1 [R=301,L]
RedirectMatch 301 ^/bournemouth-datacentre/$ http://www.example.com/blog/
And that works fine. On top of that I need to redirect (example):
OLD: blog.example.com/somecategory/some-title-here/
NEW: www.example.com/blog/some-title-here
Note that I'm loosing the category in between domain and title and replacing it with blog. That's fine.
Can anyone suggest how I can achieve both the subdomain to domain followed by blog redirect as well as the category redirects?
IMPORTANT: Old site is WP, new is Drupal!
Thanks!
You can use:
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteRule ^(?:bournemouth-datacentre|general|london)/(.*)$ http://www.example.com/blog/$1 [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteRule ^(.*)$ http://www.exmaple.com/blog/$1 [R=301,L]
Try the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^blog.example.com [NC]
RewriteRule ^(.*)$ /bournemouth-datacentre/$1 [L,R=301,NC]
I have an SSL certificate installed on my website and I would like to redirect two specific pages to https, e.g. http://domain.com/?page=credit and http://domain.com/?page=submit_form to https.
My web host added the following code to my .htaccess file, above the Wordpress settings but it still doesn't redirect the pages:
Redirect /?page=credit https://domain.com/?page=credit
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{QUERY_STRING} (^|&)p=(credit)($|&)
RewriteRule (.*) https://domain.com/?page=credit [R=301,QSA,L]
I'm sorry I'm not too good at this. Any help I can get will be appreciated.
Thanks.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{QUERY_STRING} (^|&)page=(credit|submit_form)(&|$) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{QUERY_STRING} !(^|&)page=(credit|submit_form)(&|$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]