Redirect to HTTPS with URI & without www - .htaccess

I have the same problem as the User in this thread. I basically want to redirect all http requests to https without www using htaccess.
I have implemented a solution to this problem and it does a pretty good job.
However, it does not redirect to the corresponding URI. If I visit my website on port 80 example.com/news I will be redirected to https://example.com/index.php which is basically the root page..
I really need your help guys, I totally have no clue about htaccess..
EDIT This is what my mod_rewrite looks like..
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
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]
</IfModule>

The issue probably is that you first rewrite to index.php and then make an external redirection. Try changing the order of the rules. Note that the L flag only terminates the current walk through the rule set. It is restarted after that. So first do the external direction to switch to the https protocol, then do the internal rewriting if that is corrected.

Related

Redirect http to https and www to non-www in .htaccess

First of all, I know there are lots of answers on this, but I don't actually find one that works. This is what I have in the .htaccess file right now, and I want to mention that it worked previously, but it does not anymore.
Redirect 301 /unt-de-cacao-de-plaja/filtre/producator/crisnatur/ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>
# 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]
# FORCE HTTPS AND NON WWW
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
As a mention, I will have a lot of Redirect 301 from old pages to the new ones since the entire structure has been changed.
And the links that I am redirecting inside my website come with "www" like:
https://www.example.com/unt-de-cacao-de-plaja/filtre/producator/crisnatur/
and needs to be redirected to:
https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Redirect to https and non-www
To instead redirect all requests to https and non-www, use the following code instead of the previous:
Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>
As before, place this code in the root .htaccess of your site. Here is what it's doing:
Checks if mod_rewrite is available
Checks if HTTPS is off, or if the request includes www
If either condition matches, the request qualifies and is redirected
to the https/non-www address
OR
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
A few issues, in order of importance:
You have your canonical HTTP to HTTPS and www to non-www redirects at the end of the file. By placing it at the end of the file, after your front-controller, it's simply never going to be processed for most requests. This needs to be near the start of the .htaccess file, before your front-controller.
You should avoid mixing redirects from both mod_alias (Redirect) and mod_rewrite (RewriteRule) in the same scope. Different modules execute at different times throughout the request, despite their apparent order in the config file. Since mod_rewrite is required for other redirects, you should convert the mod_alias Redirect directives to use RewriteRule instead.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
You should include the canonical scheme and hostname in your URL redirects in order to avoid multiple redirects when requesting an "old" URL at a non-canonical scheme ot hostname.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
Depending on what you mean exactly by "a lot of Redirect 301" - you should not be doing this at all in .htaccess and instead redirecting in your server-side script, once you have determined that the request will 404. This is to prioritise normal site visiters and not your redirects (that get executed on every single request).
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Since you stated that these directives worked previously then I assume the use of the HTTPS environment variable is OK on your system. But note that, whilst this is relatively common, it's non-standard. (It implies the server is using some kind of SSL front-end/proxy.)
Note that the order of these rules will result in a double redirect when requesting http://www.example.com/<anything> (HTTP + www). Which is necessary if you are implementing HSTS, but otherwise, you should reverse these two rules to avoid this unnecessary double redirect.

Redirect to HTTPS except 1 page

Been looking all over for an answer but no response found could solve my problem.
I'm using the following .htaccess file to redirect users from a site to its https version :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
# RewriteCond %{REQUEST_URI} !^/api-vop/
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I have first a rule to redirect to HTTPS then a rewrite rule for classic url rewriting to replace urls like myfile.php to /my-file
Thing is I need to let one url which is http://my-server.com/api-vop go through without being redirected to https, as seen by this line :
# RewriteCond %{REQUEST_URI} !^/api-vop/
Thing is I cannot get it to work, at best I'm trhown back to the index.php page.
Any solution anyone ?
Thanks in advance !
Keep your first rule as:
RewriteCond %{HTTPS} off
# This checks to make sure the connection is not already HTTPS
# RewriteCond %{THE_REQUEST} !\s/+api-vop/
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

htaccess subdomain rewrite keep www

I've set up wildcard domains locally for testing on .dev
I'm trying to rewrite the following URL:
http://location.domain.dev/
to
http://www.domain.dev/site/location
I would like any requests with www in the subdomain to always go to www.domain.dev but if any request is made to location.domain.dev, I would like to keep that request in the address bar (i.e i dont want people to see the underlying change)
I currently have the following in my .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.dev
RewriteRule ^(.*)$ http://domain.dev/site/%1 [QSA,NC]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Is this even possible?
You're pretty close. In order to not redirect the browser (causing the address bar to change) you need to get rid of the http://domain.dev part of the rewrite rule's target:
RewriteRule ^(.*)$ /site/%1/$1 [QSA,NC]
assuming that both *.domain.dev and www.domain.dev have the same document root. If they're different, you may have to enable mod_proxy and add a P flag so that the request gets proxied instead of redirecting the browser:
RewriteRule ^(.*)$ http://domain.dev/site/%1/$1 [QSA,NC,P]

https rewrite not working

I got the following htaccess, tried adding https rewrite/redirect - and it doesn't work, any idea what am I doing wrong?
RewriteEngine on
RewriteRule \.svn/ - [F]
# rewrite traffic to HTTPS
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Did you mean to omit the [L] after your https rewriterule? Without it, if I request "http://myserver.com/something" it will first rewrite as "https://myserver.com/something" but then (I believe) continue on in the .htaccess file, subsequently using the internal rewrite to serve up "index.php" without actually redirecting the client. Though if the file requested does exist I'm not sure how Apache would handle that.

Problems breaking an .htaccess redirect out of an infinite loop

So I have asked for help over at my weblog, scoured the internet, and pored over the examples you all have provided on here before, and I still cannot find an answer that works.
Simply put, I am trying to take all traffic referred to my site from Site A, and redirect it all to Page B within my domain. I have gotten the redirect to work perfectly, but I cannot get it to break out of an infinite loop. Any assistance would be greatly appreciated.
Code follows (though it has been "anonymized" from the specific pages I was using):
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !filename\.html$
RewriteCond %{HTTP_REFERER} ^http://online\.webpage\.com.* [NC]
RewriteRule (.*) http://www.wallsofthecity.net/year/mo/filename.html [L]
</IfModule>
Like I said, the RewriteRule works beautifully, but the first RewriteCond does not appear to be flagging when it is at the appropriate page, and just keeps redirecting folks, ad nauseum. I have been using this site: http://rexswain.com/httpview.html to check my code, and while useful, it has not given me any good answers.
Thanks for whatever help you can provide.
UPDATE:
So here is the .htacces file in its entirety, since that may make things easier:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force an external redirect to this page for referrals from that site
# This page *must* exist to prevent a loop (which it does, I checked :P)
RewriteCond %{HTTP_REFERER} ^http://mikeb302000\.blogspot\.com.* [NC]
RewriteCond %{REQUEST_URI} !=/2010/04/cruisin-for-a-bruisin.html
RewriteRule . /2010/04/cruisin-for-a-bruisin.html [R,L]
# This scenario performs no rewrite, so it should actually just be handled by
# the RewriteConds below (they won't match), but I didn't test that
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
Redirect permanent /index.xml http://www.wallsofthecity.net/feed/
Redirect permanent /rss.xml http://www.wallsofthecity.net/feed/
Redirect permanent /atom.xml http://www.wallsofthecity.net/feed/atom/
Redirect permanent /12_tribes http://www.wallsofthecity.net/category/12-tribes
Redirect permanent /as_i_say_not_do http://www.wallsofthecity.net/category/as-i-say-not-do
Redirect permanent /bigotry_exposed http://www.wallsofthecity.net/category/bigotry-exposed
Redirect permanent /commercial_appeal http://www.wallsofthecity.net/category/commercial-appeal
Redirect permanent /cowardice_on_parade http://www.wallsofthecity.net/category/cowardice-on-parade
Redirect permanent /crosscountry_jaunt http://www.wallsofthecity.net/category/crosscountry-jaunt
Redirect permanent /digital_real_estate http://www.wallsofthecity.net/category/digital-real-estate
Redirect permanent /fools_and_jesters http://www.wallsofthecity.net/category/fools-and-jesters
Redirect permanent /for_hire http://www.wallsofthecity.net/category/for-hire
Redirect permanent /me_myself_and_i http://www.wallsofthecity.net/category/me-myself-and-i
Redirect permanent /musings_of_a_madman http://www.wallsofthecity.net/category/musings-of-a-madman
Redirect permanent /one-line_review http://www.wallsofthecity.net/category/one-line-review
Redirect permanent /patron_polity_of_perforation http://www.wallsofthecity.net/category/patron-polity-of-perforation
Redirect permanent /peoples_republic_of_kalifornistan http://www.wallsofthecity.net/category/peoples-republic-of-kalifornistan
Redirect permanent /sensor_ping http://www.wallsofthecity.net/category/sensor-ping
Redirect permanent /serenity http://www.wallsofthecity.net/category/serenity
Redirect permanent /simon_jester http://www.wallsofthecity.net/category/simon-jester
Redirect permanent /the_funnies http://www.wallsofthecity.net/category/the-funnies
Redirect permanent /the_mat http://www.wallsofthecity.net/category/the-mat
Redirect permanent /things_that_go_boom http://www.wallsofthecity.net/category/things-that-go-boom
Redirect permanent /toysgizmosgadgets http://www.wallsofthecity.net/category/toysgizmosgadgets
Redirect permanent /urk http://www.wallsofthecity.net/category/urk
Redirect permanent /window_on_the_world http://www.wallsofthecity.net/category/window-on-the-world
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note: it still does not appear to be working. Test away :).
The first thing I'd try is changing the rewrite condition as follows:
RewriteCond %{REQUEST_FILENAME} !^.*filename\.html$
I think Apache is not supposed to force the match to start at the beginning of the string, but if it does (for whatever reason) and if that's the cause of your problem, this change will fix it.
As an alternative to that, just drop the first rewrite condition entirely and change your rewriting rule to
RewriteRule !year/mo/filename.html http://www.wallsofthecity.net/year/mo/filename.html
Edit: Let's try this instead (removed previous to limit post length):
External Redirect:
RewriteEngine On
# Force an external redirect to this page for referrals from that site
# This page *must* exist to prevent a loop (which it does, I checked :P)
RewriteCond %{HTTP_REFERER} ^http://mikeb302000\.blogspot\.com.* [NC]
RewriteCond %{REQUEST_URI} !=/2010/04/cruisin-for-a-bruisin.html
RewriteRule . /2010/04/cruisin-for-a-bruisin.html [R,L]
# This scenario performs no rewrite, so it should actually just be handled by
# the RewriteConds below (they won't match), but I didn't test that
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
# With an external redirect, the first RewriteCond catches our referrer redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# These are always evaluated after mod_rewrite stuff, but use the original
# REQUEST_URI unless we explicitly passed it through to handlers later in
# the chain (via the PT flag)
Redirect permanent ... (trimmed)
Internal Redirect:
RewriteEngine On
# Force an external redirect to this page for referrals from that site
# This page *must* exist to prevent a loop (which it does, I checked :P)
RewriteCond %{HTTP_REFERER} ^http://mikeb302000\.blogspot\.com.* [NC]
RewriteCond %{REQUEST_URI} !=/2010/04/cruisin-for-a-bruisin.html
RewriteRule . /2010/04/cruisin-for-a-bruisin.html [L]
# This scenario performs no rewrite, so it should actually just be handled by
# the RewriteConds below (they won't match), but I didn't test that
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
# With an internal redirect, we have to do an extra check to prevent this rewrite
# with our referrer redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0 !-f
RewriteRule .* /index.php [L]
# These are always evaluated after mod_rewrite stuff, but use the original
# REQUEST_URI unless we explicitly passed it through to handlers later in
# the chain (via the PT flag)
Redirect permanent ... (trimmed)
Edit (again):
Let's try a little diagnostics too...Can you put this above your other rules in the .htaccess file?
RewriteCond %{QUERY_STRING} diagnostic
RewriteRule . /?ref=%{HTTP_REFERER}&uri=%{REQUEST_URI}&matchable=$0 [R,L]

Resources