Unable to make a 301 domain redirection - .htaccess

Hi I have an application which uses opencart. I would like to make a 301 reditect in case the user types http://example.com. To be redirected in http://www.example.com (301 status code)
Here is my .htaccess content:
RewriteEngine On
\#OPENCART REWRITES START
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
\#OPENCART REWRITES END
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
I get 302 redirection instead of 301.
Thanx,
Granit

Have you tried doing:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Emphasis on the second line, as it matches against http://example.com as opposed to matching against anything-but www.example.com, which will break if you happen to use subdomains. I'm not sure if this is exactly related to your 301/302 issue, but it could have an affect. Also, try on your Rule [R=301,NC,L].

Try it with a different order. Put your rules that cause an external redirect before those that only cause an internal redirect:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#OPENCART REWRITES START
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
#OPENCART REWRITES END

Related

htaccess RewriteRule and Redirect 301 interfering with one another

My .htaccess file includes this, which allows the URL structure in my ExpressionEngine site to function:
RewriteRule ^(.*)$ /index.php?/$1 [L]
However, it is interfering with what I'm trying to do here, redirecting an old page:
Redirect 301 /old-url/ /
This is what I see in my browser address after I load the /old-url/ address:
https://www.domain.test/?/old-url/
If I take out the RewriteRule, my Redirect 301 works, but all the navigation in the site breaks. Is there some way for me to make an exception where I want to redirect old URLs? I have inherited someone else's work in this case.
Edit: here is my entire .htaccess file content:
RewriteEngine On
Redirect 301 /in-loving-memory-of-our-partners /
Redirect 301 /in-loving-memory-of-our-partners/ /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^sitemap\.xml$ /sitemap/xml [NC,L]
RewriteCond $1 !^(assets|emergency|portalimages|images|system|themes|test|AUL|swfs|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule ^(.well-known) - [L]

Modifying my htacess to redirect to SSL URL

My current .htaccess looks like this:
Redirect 301 /~mysite-net/vlog http://www.example.net/vlog
Redirect 301 /~mysite-net/pp https://www.example.net/pp
Redirect 301 /~mysite-net/pp02 https://www.example.net/pp02
Redirect 301 /~mysite-net/pp03 https://www.example.net/pp03
Redirect 301 /~mysite-net/ http://www.example.net/
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+).html$ https://www.example.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ index\.php\?s=([^\s]*)
RewriteRule ^/?(.*?)/?$ %1?%2%3 [L,R=301]
this does what intended so no problem. However, now I need to modify it so it always redirects to the SSL version of my site. It means no matter if the user types http it will always redirect them to https instead.
So I just added these lines at the bottom:
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]
but it's not working. What am I missing?
May be this can be work
I think you forgot to put RewriteCond %{HTTPS} !on
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
So I just added these lines at the bottom:
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]
There are two problems with your existing directives:
You have put them in the wrong place in your file - they need to go at the top, not the bottom. By placing them at the end of the file they will only get processed when requesting static resources, because the preceding directives will otherwise "catch" the URL.
You are trying to combine the non-www to www redirect in this directive as well, however, you have omitted the OR flag on the first condition, so it will never redirect requests for http://www.example.net/foo.
In other words, at the top of your file.
RewriteCond %{HTTP_HOST} ^example\.net [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.example.net/$1 [R=301,L]
The RewriteCond %{HTTPS} !on condition mentioned in the other answer is simply an alternative to your SERVER_PORT condition.

Avoid infinite loop with 301 redirects

I'm using .htaccess to redirect
http://www.example.com/foo/
to
http://www.example.com/foo/bar
This is my code:
redirect 301 /foo/ http://www.example.com/foo/bar
However this produces a feedback loop, something like
http://www.example.com/foo/barbarbarbarbarbar etc.
I've tried placing delimiters around it:
redirect 301 ^/foo/$ http://www.example.com/foo/bar
but then the redirect simply doesn't take place. I'm probably missing some very simple point of syntax. Any ideas? Thanks.
EDIT
Here's my (almost) full .htaccess file:
RewriteEngine On
RewriteBase /
# Canonical is www version
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#redirect => http unless special page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(javascripts|images|library|stylesheets)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#redirect => https for special pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# send to router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Have your rules like this:
RewriteEngine On
RewriteBase /
# Canonical is www version
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
#redirect => http unless special page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(javascripts|images|library|stylesheets)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#redirect => https for special pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} off
RewriteRule !^index\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^foo/?$ /foo/bar [L,NC,R=301]
# send to router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
make sure to test this in a new browser or clear your browser cache before testing.
For your information, it really depends on your hosting provider. It may be behind a Load Balancer and you don't have the proper env var set (like HTTPS and others...).
In my case (Infomaniak), nothing actually worked and I got infinite redirect loop.
The right way to do this for Infomaniak is actually explained in their support site:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://your-domain.com/$1 [R=301,L]
So, always check with your hosting provider. Hopefully they have an article explaining how to do this. Otherwise, just ask the support.
It depends on what is the resource that you want to expose with your redirect.
Is it a file or a directory.
if you would expose a directory under bar you should try :
RedirectMatch 301 ^/foo/ http://www.example.com/foo/bar/
if you would redirect to a file you should use this syntax :
redirect 301 /foo http://www.example.com/foo/bar
you can see this post for more informations
Instead of using redirect, you can use passthrough.
RewriteRule ^/foo$ /foo/bar [PT]

.htaccess not redirecting from old url to new url

i have developed a website using codeigniter. Previously the site had a long URL structure so i have made them shorter in the new website.
Although i used the redirect directive in htaccess it gives me a 404 error. I have removed all the old controllers and functions.
below is a few lines from htaccess (there are many urls redirecting to new ones)
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
#RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Redirect 301 /payments/charity_and_donations/paid http://www.mysite.com/charity
Redirect 301 /outwards/office_furniture/damaged http://www.mysite.com/office_assets
Redirect 301 /funding/business_and_person/inward http://www.mysite.com/funds
can someone tell me why it is not redirecting from the old to the new and what am i doing wrong?
You need to have your redirecting happen before you route URIs to /index.php. Also, since Redirect is part of mod_alias and your other redirect/routing rule is mod_rewrite, the URI is being processed twice when it's not supposed to. You should just use mod_rewrite and add the rules to the beginning:
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteRule ^payments/charity_and_donations/paid http://www.mysite.com/charity [R=301,L]
RewriteRule ^outwards/office_furniture/damaged http://www.mysite.com/office_assets [R=301,L]
RewriteRule ^funding/business_and_person/inward http://www.mysite.com/funds [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Try the below code, this works perfectly for me
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

Force HTTPS on rewritten url via htaccess

I have a website on www.domain.com and here is the root (and only) .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I'm basically doing rewrite on my URL's so everything is going through the index.php page.
How can I force HTTPS (SSL) on www.domain.com/login, /register and /settings? So when I try to access the http://www.domain.com/login I should be immediately redirected to https://www.domain.com/login?
I searched SO and I found for folders so if I have folder /login it would work, but not for me in this case of already rewritten URLs.
Using mod_rewrite you should be able to do something like this
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} ^/register
RewriteRule (.*) https://www.domain.com/register [R]
Try:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^login$ https://www.domain.com/login [R=301,L]
RewriteRule ^register$ https://www.domain.com/register [R=301,L]
RewriteRule ^settings$ https://www.domain.com/settings [R=301,L]

Resources