I am trying to use SSL on my cakephp 3.1 website but I always ends up with redirect loops.
My website is on a shared hosting server. I have configured it as follow:
├───public_html
├───.htaccess (1)
├───website
│ ├───.htacess(2)
│ └───webroot
│ └─── .htaccess(3)
In my root .htaccess (1) in the public_html folder I added the following to set my document root
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^website.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.website.com$
RewriteCond %{REQUEST_URI} !website/webroot/
RewriteRule (.*) /website/webroot/$1 [L]
My second .htaccess (2) in the website dir looks like the following
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
and the last .htaccess in the cakephp webroot folder is
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
So basically the only thing I've added is the public_html .htaccess to set my document root, other files i did not touch
To force SSL I've added the following to the pubic_html .htaccess file (1)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But still no luck. No matter what I do I either ends up with redirect loop or the site just break down.
Help please :)
Related
I've built a php website working all fine with all the redirects except for http:// versions.
So I'm trying to edit my main .htaccess file to prevent http:// showing the error "NOT SECURE", and forward the user to https://
I've used a framework called TraversyMVC while building my website and it has the setup below in the public_html directory .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And this is my .htaccess file in the /public directory
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
This is what I've tried in my public_html directory .htaccess file but it didn't work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} public/ [L]
RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
</IfModule>
Looking for the correct solution.
Thanks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} public/ [L]
RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
You are redirecting the request before the internal rewrite to the /public subdirectory (so the second condition that checks for public/ will never match) and /public should not be in the visible URL you are redirecting to. You should also be explicit that this is an external redirect, not an internal rewrite. (As written this rule would result in an implicit 302 - temporary - redirect.)
This needs to go before the existing rules in the root .htaccess file. Try the following instead:
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
You do not need the <IfModule> wrapper.
I'm struggling with the .htaccess file. I've wordpress installed in a subdirectory 'wordpress'. In the root folder if have the htaccess with the following content:
Order Allow,Deny
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule !^wordpress/ /wordpress%{REQUEST_URI} [L,R=301]
</IfModule>
Redirection is working, but how can I hide the subfolder 'wordpress'?
THX in advance
EDIT: Tried following content now but still not working:
root htaccess:
Order Allow,Deny
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule !^wordpress/ wordpress%{REQUEST_URI} [L]
</IfModule>
wordpress htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
EDIT2: my whole root htaccess looks like this now:
Allow from all
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule !^wordpress/ wordpress%{REQUEST_URI} [L]
If i type in www.example.com I am redirected to example/wordpress/home, example/wordpress/contact and so on...
I would like to hide the wordpress directory like example/home, example/contact and so on
Redirection is working, but how can I hide the subfolder 'wordpress'?
You shouldn't be "redirecting". You should be internally rewriting the request instead. Remove the R flag.
For example:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule !^wordpress/ wordpress%{REQUEST_URI} [L]
The slash prefix on the susbtitution string is also not required.
This assumes you have the standard WP .htaccess file in the /wordpress subdirectory.
UPDATE: Also confirm you have removed the /wordpress subdirectory from the "Website Address" and "Site Address" in WordPress General settings.
My Laravel project is already inside in shared hosting server(i know what do you thinking...). And inside root folder i got a .htaccess file which has this code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteCond $1 !^(public)
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
This gives me good URL's without "public" but there is one case:
If i go:
www.example.com
so it always stays with URL www.....If i go:
https://www.example.com
it always stays with URL https://....
My question:
How to make that the URL would always be https://www.example.com whatever I enter www or https first?
This should do:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can do that by putting following lines in the .htaccess files
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^.*$ https://%{HTTP_HOST}/public%{REQUEST_URI} [L,R=301]
</IfModule>
Put the code in .htacces in your laravel root directory, it should do the job:
RewriteEngine On
# enforce https
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
# use public directory as root, but don't include it in url
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
I've got a duplicate content problem with my Symfony2 projet. The following urls gives the same content :
www.mywebsite.com/web/page and www.mywebsite.com/page
Here is the content of my /.htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>
And the content of my /web/.htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I would like to redirect any url starting with /web to / but I can't manage to do it. Do you have any suggestion ?
In the htaccess file in your web directory (the /web/.htaccess file), add these rules right beneath the RewriteEngine On:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /web/
RewriteRule ^(.*)$ /$1 [L,R=301]
This redirects all direct access to the web directory to the root.
In my website root I have the following to redirect to non www domain
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
in a subfolder named 'photography' I have this...
RewriteEngine on
RewriteRule ^show/([^/\.]+)/([^/]+)$ show.php?section=$1&photoid=$2 [L]
Anything inside the photography folder ignores the www removing rule. How do I get these two rules to both apply to folders/files within the photography folder?
Also... my root htaccess file has this...
# 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>
# END WordPress
Could it be interfering? I have a self-hosted wordpress blog but it's not in the root of the website, it's in a subfolder called 'blog' so I don't know why this rule is in my root's htaccess file. Can/should I move it?
Edit: Just to point out, in case it isn't obvious - I'm a complete noob when it comes to htaccess and mod_rewrite stuff. Does a htaccess file in a subfolder override any htaccess files nearer to the root than it? Or do the htaccess contents combine?
Edit 2: I have tried moving the second rule to the same htaccess file as the www removing rule as per the following code...
# 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>
# END WordPress
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ photography/show.php?section=$1&photoid=$2 [L]
If I then go to one of my photography pages it resolves to the intended url (http://notails.com/photography/show/pointofayre/260 for example) but the page is a 404.
If I manually add 'www' to that it undoes the other rule... (http://notails.com/show.php?section=pointofayre&photoid=260) and removes 'photography/' from it.
Add RewriteOptions inherit into .htaccess in your "photography" folder because right now all rewrite rules from parent folders are ignored (default behaviour).
Alternatively move rewrite rules from that subfolder .htaccess into root one (you will need to slightly modify your rule by fixing the path -- adding photography/ may be enough, depends on actual "photography" location)
UPDATE:
Your root .htaccess can be like this:
Options +FollowSymlinks
<IfModule mod_rewrite.c>
# activate rewrite engine
RewriteEngine On
# we are in the root
RewriteBase /
# no www please
RewriteCond %{HTTP_HOST} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,L,QSA]
# photography
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ /photography/show.php?section=$1&photoid=$2 [L]
# WordPress rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>