.htaccess Adding HTTPS forcing to existing rewrite rules of subdirectory - apache

Probably I'm overseeing something very basic, htaccess is not my field of expertise.
My site is https-only, to achieve this, the root folder / contains these lines:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R=301,L]
This works just fine. Now we have a new folder called /projects, which contains its own htaccess rules to translate the url in a database query by a file called /projects/project.html
The working htaccess within the folder /projects is:
RewriteEngine On
RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
Now I would like to force https connection on this one, because the https rule in the main directory is not applied on /projects if you visit by http://URL directly. Therefore I modified the file to:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://example.com%{REQUEST_URI} [R=301,L,NE]
RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
With this, it switches to https, but I keep getting "ERR_TOO_MANY_REDIRECTS".
When I change the lines as #thickguru suggested, like this:
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
It will redirect me to https://example.com/index.html right away.
I fiddled around with the settings, also with the information of this post, however still I can't get it to work.

I just noticed it the variable you are matching against is %{ENV:HTTPS} , so , you can use the following rule in /projects/.htaccess :
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Clear your browser cache or try a diffrent browser to test this.

Use this in your .htaccess:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
Also, place this above your your other RewriteRule, so that your .htaccess will look like:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
RewriteBase /projecten/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
EDIT - Try this:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Related

Redirect 301 appending old URL with New URL

I am having a problem while redirecting a page whose URL was changed but I want old URL to be redirected to new. Here is .htaccess file
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Using mod_rewrite in the last line I am not getting redirected to new URL but when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended to in it in this format.
https://example.com/contact-us/?/contactus/
I have searched Stack Overflow for solution to this problem and tried many similar scenarios but that didn't work.
Have it like this the given order.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^contactus/$ /contact-us/ [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/system [NC]
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
RewriteEngine On is not required multiple times in same .htaccess.
Make sure to clear your browser cache before testing the change.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
Your mod_rewrite external redirects (the last three rules) are in the wrong place. These need to be before the internal rewrite (front-controller). By placing them at the end of the file they are never going to get processed for any URL other than static resources, since all other URLs are routed to index.php, at which point processing stops.
when I use mod_alias(Redirect) rule I get redirected to new URL but Old URL gets appended
Different modules are processed independently regardless of the order of the directives in the config file. mod_alias is processed after mod_rewrite - but it is processed. However, the Redirect directive is prefix matching and everything after the match is appended onto the end of the target URL.
And there's no need to repeat the RewriteEngine directives
Have your directives like this:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteRule ^contactus/$ http://example.com/contact-us/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/system
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
And make sure to clear your browser cache before testing. Preferably test with 302 (temporary) redirect to avoid caching issues.

How do I edit apache .htaccess correctly?

Good day! There are 3 tasks that I want to implement using the .htaccess file:
Work only with https protocol
Create a 301 redirect from https://www.example.com to https://example.com
Redirect all requests to the index.php file
My best version of .htaccess looks like this, but I'm not sure if this is the right decision:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} .* [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
I would be grateful for any advice!
Could you please try following, written as per your shown samples. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
##For applying https on each request.
RewriteCond https !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
##For removing www from request.
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
##For applying index.php for non existing files or directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

htacces opencart redirect https not work corectly

After I redirected to https some of the links no longer work:
ex:
domain/category - it's ok
domain/product - it's ok
domain/category/product - not work
domain/category/category/product - not work
It looks like the page doesn't load CSS, just links.
Can anyone help me with an advice, code, example to solve this problem?
Thank you very much.
RewriteEngine On
RewriteBase /
### Ptr SSL
#RewriteCond %{HTTP_HOST} !^www.
#RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#end SSL
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=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]
Here's how I have one of my sites:
RewriteBase /
# SSL & www
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# end SSL & www
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^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]
I also have "SSL" set to "Yes" in the Settings as well as SEO URLs enabled. I know the two Rewrite Conditions could be condensed but my site breaks when I do that so I'm happy to leave them as is
Edit Having seen your comment with the site URL, the issue doesn't appear to be the .htaccess but instead the page's <base> element's src value is "(unknown)" this is the element that contains the base URL of the site for all relative links.
So the 404 errors were indicating that the browser is assuming the "SEO-friendly" category name is a real subfolder and that the css files are stored there. In other words it thinks that there is a folder called "public_html/Accesorii" and the css files are there.
What you need to do is look in the theme settings or manually edit the head.php file, it should be somewhere in /catalog/view/theme/fastor/common/ folder.

Remove .php extension, force non-slash, force non-www, force https with .htaccess

I have been looking into this for quite a while now, but i just can't get it right. When I have one part working (e.g. forcing non-php), the other part stops working (e.g. forcing non-slash).
What I want exactly:
remove .php extension (so example.com/foo.php becomes example.com/foo in the browser url bar)
remove the slash at the end of a URL (so example.com/foo.php/ becomes example.com/foo. BUT when it's a directory with an index.php inside, it has to redirect accordingly (i can't get this last part to work)
force non-www (this works)
force https (this works)
What I my .htaccess looks like right now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I experience the following problems with this:
example.com/foo.php/ redirects to example.com/foo.php instead of example.com/foo
example.com/foo.php/foo displays the example.com/foo.php file, while example.com/foo/foo displays a 404 message (the latter would be correct in my case)
example.com/admin (this is a folder with an index.php in it) displays a 404 message
I appreciate your help!
Okay, so I know it is a broad question. But if anyone ever searches for this, I almost have it (the only thing that doesn't work is redirecting domain.com/foo.php/foo to domain.com/foo/foo
Here is the .htaccess file I have right now:
RewriteEngine On
Options -Indexes
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
RewriteRule ^ /%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess - redirect to https not working

Per: https://exp-resso.com/blog/post/2011/08/securing-your-expressionengine-website-with-https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond $1 ^(member|account|checkout|system) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This tells your server “If HTTPS is off, and the request starts with
member OR account OR checkout OR system (not case sensitive), redirect
to https://current-domain/current-page”. It’s a nice simple method of
locking down entire subfolders / template groups.
I've added this to my htaccess file like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond $1 ^(sign-in|sign-up) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
However, when I go to my http://mydomain.com/sign-in, the URL doesn't change to https://mydomain.com/sign-in. Any idea what's wrong?
EDIT 1:
My htaccess also has the following (to remove "www") and I wonder if having both might be causing the problem?
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
EDIT 2:
Process of elimination, it turns out this is causing the problem:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
When I comment out the RewriteRule, the https:// is forces. What's causing the conflict?
Try to put (sign-in|sign-up) condition inside RewriteRule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(sign-in|sign-up)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=301]
What about this? (If port == 80 then redirect )
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} member [OR]
RewriteCond %{REQUEST_URI} account [OR]
RewriteCond %{REQUEST_URI} checkout [OR]
RewriteCond %{REQUEST_URI} system
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
First make sure that rewrite works on your server and that the htaccess is read (e.g. by issuing a redirect on every URL).
Then use RewriteCond %{REQUEST_URI} ^/(sign-in|sign-up) instead of RewriteCond $1 ^(sign-in|sign-up) [NC]
It works and is easier to read too
So you htaccess should look like this
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/(sign-in|sign-up) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]