Using htaccess I want to force http to https and at the same time redirect to www. and to the 'public' folder where my application files are.
The result should be that http://example.com should redirect to https://www.example.com/public/
To redirect to https and www I manage by using:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
To redirect to the 'public' folder I manage by using:
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
Now I need to combine these but my many attempts did not result in a working redirect and I would need some advise on this.
EDIT:
In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
<<
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
The REQUEST_URI server variable starts with a slash, so the condition above will never match. For example:
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) public/$1 [L]
However, assuming you have another .htaccess file in the /public subdirectory I would write this block like the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) public/$1 [L]
This has the added benefit of allowing a public path segment at the start of your URLs (if you wish). eg. The URL /public/foo would internally rewrite to /public/public/foo without creating a rewrite loop.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
You are missing an OR flag on the first condition. For example:
RewriteCond %{HTTPS} off [OR]
:
Without that, it will fail to redirect http://www.example.com/ to HTTPS.
Just to clarify, the canonical redirect should go before the internal rewrite to the public directory.
UPDATE:
EDIT: In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
The substitution string on the last RewriteRule directive is incorrect. You need to remove the slash prefix from /index.php. It should just read index.php. Otherwise it is going to rewrite the request back to the document root (filesystem path) and result in either an endless loop or a 404 (depending on the parent directives). By changing it to index.php (no slash) it is now relative to the /public directory (the directory that contains the .htaccess file), instead of the document root.
You can also simplify the RewriteRule pattern and the NC flag is not required here. So, in summary, the /public/.htaccess file should read:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]
Related
In my Root folder I have index.php and profile.php.
Currently I'm using these rules for rewrite my index.php
from example.com/index.php?a=profile&u=user1 to example.com/profile&u=user1
RewriteEngine On
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome [NC]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC]
It works fine but now I want to rewrite my URL for profile.php page too, like this:
from example.com/profile.php?u=user1 to example.com/user1
How can I edit my .htaccess to rewrite profile.php together?
Based on question and comments above here are the rules that should work for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^profile/([^/]+)/?$ profile.php?u=$1 [NC,QSA,L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]
I need help with an .htaccess that:
forces http:// to https://
AND
forces .html to /
What I have so far:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Options -MultiViews
RewriteEngine On
#1 This line checks if the https is off
RewriteCond %{HTTPS} ^off$
#then, redirect to https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R]
#2 this line checks if the request is /file.html
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
#then redirect /file.html to /file
RewriteRule ^ /%1 [NC,L,R]
#3 if the request is not for dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing filename
RewriteCond %{REQUEST_FILENAME}.html -f
#then rewrite /file to /file.html
RewriteRule ^([^/]+)/?$ $1.html [NC,L]
In the example above, the first condition is met when the original scheme is HTTP, and then the rule is processed. HTTP goes to HTTPS. The first Round of rewrite processing ends here.
In the second round, mod_rewrite accepts the URI /file.html and the rule redirects it to /file, since the /file does not exist in directory, so we need to rewrite it to the original file #3...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I've used both of them before, but I haven't used them together. Hope this helps.
(EDIT)
To actually FORCE extensions to look like directories:
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file
First of all, make sure mod-rewrite is enabled.
Then, put this code in your htaccess (which should be in document root folder)
Options -MultiViews
RewriteEngine On
RewriteBase /
# Redirect http urls to https equivalent
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://{HTTP_HOST}/$1 [R=301,L]
# Redirect existing /path/file.html to /path/file/
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+?)\.html\s [NC]
RewriteRule ^ %1/ [R=301,L]
# Internally rewrite back /path/file/ to /path/file.html (if existing)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+)/$ $1.html [L]
Warning:
Make sure to serve the same document root as http for https (apache ssl block configuration)
Be aware that this creates virtual directories (by adding a trailing
slash) which could mess up your html resources (if you're using
relative paths instead of absolute paths). If so, use absolute paths
instead
I have a super basic redirect request I like to get done, but i mess it up every time i give it a try, the best so far results in a double www.www redirect (http://www.www.domain.tld/)
My .htaccess has 'two' rules i like to get checked and done.
1st: Redirect all non existent files to the root folder
2nd: Add www. if domain requested as non-www
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) http://www.%{HTTP_HOST}/? [L]
#Rewrite for non www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/? [R=301,L]
I have changed the first Ruleset, but never worked out
No. 1
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) http://%{HTTP_HOST}/? [L]
Ended with a redirectloop
No. 2
# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) /? [L]
Ended like no rule was even given.. so an error 404 site with the unchanged URL http://www.domain.tld/123456789.html
Where am i going the wrong way?
You cannot use http:// while doing silent rewrite otherwise mod_rewrite will convert that to an external redirect. Have your rules like this:
DirectoryIndex index.php
RewriteEngine On
#Rewrite for non www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# If requested resource is not a file or directory then redirect to /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R=302]
Make sure to clear your browser cache before testing this.
I am new to the whole htaccess language, but I feel like I am close to what I want.
Facts:
parts is a real folder/directory
anotherpart is a real folder/directory
there are no other files in <root> other then index.php
there are no files in the parts folder/directory
Folder/Directory structure:
<root>/index.php
<root>/parts/anotherpart/this.php
What .htacces I have running:
RewriteEngine On
RewriteBase /
RewriteRule ^parts/anotherpart/(.*)$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.somewebby.it$ [NC]
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]
Results:
always having https and no www
hiding parts/anotherpart and showing https://somewebby.it/this.php
The problem:
The requested URL /this.php was not found on this server.
You can replace your current code by this one in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteBase /
# if "www" or http -> redirect to https://domain.com/xxx
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]
# hide "parts/anotherpart/"
RewriteCond %{THE_REQUEST} \s/parts/anotherpart/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
# silently rewrite back to "parts/anotherpart/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/parts/anotherpart/$1 -f
RewriteRule ^(.+)$ parts/anotherpart/$1 [L,QSA]
this is my problem:
Goal:
redirect all non-www queries to www (permanent redirection if possible), AND also all queries to index.php (for proper work of Zend framework)
Issue:
.htaccess not working as I would like it to work
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule !^www\.(.*)$ http://www.%{HTTP_HOST}/$1 [NC]
RewriteRule ^www\.(.*)$ http://%{HTTP_HOST}/$1 [NC]
RewriteRule ^.*$ index.php [NC]
RewriteRule ^.*\.index.php ./ [NC,L]
SetEnv APPLICATION_ENV development
To redirect to www try this:
RewriteCond %{HTTP_HOST} ^yourdomain\.co\.uk
RewriteRule ^(.*)$ http://www.yourdomain.co.uk/$1 [R=permanent,L]
To map all queries to index.php try this:
RewriteRule ^(.*)$ /index.php [QSA,L]
Here's more information about what the R, QSA & L are used for:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule