A while ago, I asked a question about some .htaccess rules.
Somebody was really kind and gave me the following:
RewriteEngine On
RewriteBase /
# Force https and non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Redirect /index.htm to /home and avoids infinite redirect loop
RewriteCond %{THE_REQUEST} \s/index\.htm\s [NC]
RewriteRule ^ /home [R=301,L]
# Rewrite (internally) /home to /index.htm
RewriteRule ^home$ /index.htm [L]
# Redirect (if not an existing file) to /index.htm (which will after redirect to /home)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.htm [R=301,L]
I thought it worked perfectly fine, but I have some issues:
https://example.com/
https://example.com/home
https://example.com/index.htm
https://www.example.com/
https://www.example.com/home
https://www.example.com/index.htm
- all variants redirecting to https://example.com/home working fine
http://example.com/ -> https://home (in Chrome) / error (in Firefox)
http://example.com/home -> https://home (in Chrome) / error (in Firefox)
http://example.com/index.htm -> https://index.htm
http://www.example.com/ -> error
http://www.example.com/home -> https://home (in Chrome) / error (in Firefox)
http://www.example.com/index.htm -> https://index.htm
- not working at all
I think it is maybe caused by the [L] flags, but I can not imagine how to solve this problem.
Thanks for your help!
There is a problem is in your first rule because you are attempting to capture value from a RewriteCond after OR clause.
You can use these rules:
RewriteEngine On
RewriteBase /
# Force https and non-www
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
# Redirect /index.htm to /home and avoids infinite redirect loop
RewriteCond %{THE_REQUEST} \s/index\.htm\s [NC]
RewriteRule ^ /home [R=301,L]
# Rewrite (internally) /home to /index.htm
RewriteRule ^home/?$ /index.htm [L,NC]
# Redirect (if not an existing file) to /index.htm (which will after redirect to /home)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /home [R=301,L]
Make sure to clear your browser cache before testing this change.
Related
My .htaccess file content
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Remove index.php
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
# Redirect Http To Https
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect Non Www To Www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
my example.com redirected well to www.example.com (Not problem)
my problem when I go to other pages like
example.com/page1/test redirected to www.example.com
(That my problem) I need it redirect me to www.example.com/page1/test
Your directives are in the wrong order. You need to put your canonical redirects before the front-controller - at the top of the file.
By placing the redirects at the end they are simply never going to get processed for anything other than static files/directories since everything else is routed to index.php.
Your HTTP to HTTPS redirect is also incorrect since you are unconditionally prefixing with the www subdomain.
Also, no need to repeat RewriteEngine throughout the file.
In other words, arrange your directives like this (assuming you are not intending to implement HSTS):
Options +FollowSymLinks -Indexes
RewriteEngine On
# Remove index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
# Redirect Non Www To Www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
# Redirect Http To Https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
It is more efficient to use a regex like ^ to simply be successful for every URL, rather than .* that needs to traverse the entire URL-path - since you are not capturing this.
You will need to clear your browser cache before testing. Preferably test with 302 (temporary) redirects to avoid caching issues.
I want to force redirect OpenCart store to its WWW version with opencart.
SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is the htaccess part for the redirect and it works but the smart URL now are
https://www.example.com/index.php?route=the-name-of-the-product
and it should be
https://www.example.com/the-name-of-the-product
Any idea how to fix this problem with the smart urls?
Am I doing the redirect wrong or the problem is other?
Try moving the following lines at the start of .htaccess file
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I see that all these scripts for removing .html extension seem not to work via SSL. I solved the issue with new script but now i am creating a 2 step redirect chain which i do not like for SEO reasons - and with your help :) i hope to get it back to a redirect step / hop.
My script is
RewriteBase /
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.carpro.ro/$1 [R=301,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
but it seems to create a cascade of redirects.
Is there any better version which
301 http to https for all pages of the site
redirect non WWW to WWW
remove .html extension and do not leave a trailing /
The script above works but with redirect cascades which i do not like
http://www.domain.com/something.html
does a 301 Redirect
https://www.domain.com/something.html
then again a 301 Redirect
https://www.domain.com/something
rather than
http://www.domain.com/something.html
a single redirect to
https://www.domain.com/something
Any ideas on optimising this?
Try this code:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.carpro.ro%{REQUEST_URI} [NE,R=302,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.+?)\.html$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.html [NC,L]
You can try below Rewrite Rules to eliminate html extension
RewriteRule ^(.*).html$ /$1 [R=301,L]
And the following rewrite rule for trailing / removal
RewriteRule ^(.*)/$ /$1 [R=301,L]
Hope this helps
I am trying to rewrite form http to https and www to non-www urls.
Basically if someone goes to http://www.example.com they need to be redirected to https://example.com and all other instances of wrong urls (http://example.com, etc.).
I tried to do this by piecing suggestions together:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^www\.garrysun\.com [NC]
RewriteRule ^(.*)$ https://garrysun.com/$1 [L,R=301]
But when I check the URL in a redirect checker I get errors... http://garrysun.com works fine and goes to https://garrysun.com but http://www.garrysun.com gets this:
Checked link: http://www.garrysun.com
Type of redirect: 301 Moved Permanently
Redirected to: https://garrysun.com/https://www.garrysun.com
How can I correct my rewrite code?
P.S. - I am using OpenCart 1.5.4 and I also have the following rewrite code above the new stuff (don't know if it affects anything):
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
You can use this single rule for that requirement:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://garrysun.com%{REQUEST_URI} [NE,L,R=301]
# rest of your rules will appear below
I've tried all the answers to similar stack questions and nothing has worked. I need to redirect all to https://www except for example.com/blogs/* and example.com/page-name.
I currently have this:
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
which redirects everything except for https://example.com, it will NOT add the www.
You can see for yourself at https://moblized.com
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L]
RewriteCond %{http_host} ^moblized.com [NC]
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} blogs
RewriteRule ^(.*)$ http://moblized.com/blogs/$1 [R,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
# $Id: .htaccess,v 1.90.2.4 2009/12/07 12:00:40 goba Exp $
AddHandler php5-script .php
Thank you!
I hope I understood you correctly. You want:
redirect from example.com to www.example.com (except /blogs/ and /page-name)
redirect all pages to HTTPS (except /blogs/ and /page-name)
based on your current .htaccess under /page-name you mean /favicon.ico
Here are the rules for the above requirements -- put them into your .htaccess:
# activate rewrite engine
RewriteEngine On
# don't touch favicon.ico (always accept as is regardless of the domain or protocol)
RewriteRule ^favicon.ico$ - [L]
# don't touch /index.php (usually means already overwritten URL)
# otherwise we may enter into a loop
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^index\.php$ - [L]
# ensure trailing slash is present for /blogs -> /blogs/
RewriteRule ^blogs$ http://mobilized.com/blogs/ [R=301,QSA,L]
# /blogs/ should only be accessible via http://example.com/blogs/
RewriteCond %{HTTP_HOST} !^moblized\.com$ [NC]
RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L]
RewriteRule ^blogs/.* - [L]
# redirect to www.example.com if necessary
RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC]
RewriteCond %{REQUEST_URI} !=/client-ipad-contest
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L]
# redirect to HTTPS if not there already
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !=/client-ipad-contest
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
BTW, browser most likely will show "Untrusted Certificate" warning if your customer go to https://example.com. This is because HTTPS session has to be fully established first before the request starts processing by Apache's rewrite module.
If that is problem -- then consider buying another SSL certificate (or from another vendor) which will cover both example.com and www.example.com (GoDaddy does this for sure) or get wildcard certificate which will cover all subdomains -- *.example.com (but this most likely will be much more expensive).
UPDATE: After simulating your requirements locally (sorry, I have no SSL with working Apache, so I have replaced it (in my testing) with different kind of rule/domain name) I have revised and updated the rules.
I've tested these rules locally (all pages are very simple, just include 1 image & css and a bit of text) -- everything looking good. Let me know if something does not work.