I have tried some different things but can't get this working.
What my code should do:
remove www
use https
if the url doesn't exist go to index.php
1&2 work for me
3 works for me
But when I add 1,2 & 3 together I get an error.
My code is:
RewriteEngine on
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 %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ /index.php [L]
Thank you :)
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* https://%1/$0 [R=301,END]
RewriteCond %{HTTPS} =off
RewriteRule .* https://%{HTTP_HOST}/$0 [R=301,END]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ index.php [NS,END]
Using a base is a good idea. Extra groups are not needed in PCRE. Only redirect non-empty URLs as you likely have index.php in your DirectoryIndex already.
Related
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists
# redirect to the physical page
RewriteRule ^(.*)$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^ /serve.php [L]
RewriteRule ^(\w+)$ ./serve.php?id=$1
So in the first part of the code I just turn http into https. My aim is that I can use urls without an .php but in my old code I had the problem that if I did so, it was used as an id for my serve.php page. So if use https://example.com/contact it was like https://example.com/serve.php?id=contact but I want it to work as https://example.com/contact.php but on the other side I want that ids that arent directions or file should still work as ids. My old code was...
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./serve.php?id=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can use these rules in your site root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# skip rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite to the physical php page
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^/?$ serve.php [L]
RewriteRule ^(\w+)/?$ serve.php?id=$1 [L,QSA]
Currently I am working on an Angular 4 project for my work. The one thing I have 0 experience in is the .htaccess file.
Because we are using Angular, we are have the following rewrite conditions:
## Normal Rewrite
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
However, I would like to have a redirect to maintenance.html if we are under maintenance.
I have found the following code for that:
## Maintenance rewrite
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=302,L]
Is it possible to have something like:
Maintenance: true
If maintenance === true {
// Execute maintenance rewrite
} else
// Execute normal rewrite
Thanks in advance.
I'm confusing with my htaccess configuration. This is my site's structure:
\www
\public
\css
\js
In www directory, I have a htaccess like this:
RewriteEngine On
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www\.example\.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{REQUEST_URI} !^/public/.
RewriteRule ^(.*)$ /public/$1 [NC,L]
And this one is in public directory:
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [NC,L]
When I access css directory, browser address bar will shows in 2 different ways:
www.example.com/css -> www.example.com/public/css/
www.example.com/css/ -> www.example.com/css/
Anyone can explain to me this difference and how can I configure to make both of them will return the second result www.example.com/css/
This is happening due to mod_dir adding a trailing slash after your rewrite rules. Have your root .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R,L,NE]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{DOCUMENT_ROOT}/$1 !-d [NC]
RewriteCond %{DOCUMENT_ROOT}/$1/ !-d [NC]
RewriteRule ^((?!public/).*)$ public/$1 [NC,L]
I use https:// for some subdirectories (/login, /admininstration), for this I find some examples - but on the other hand I've to ensure, that all other pages redirect to http://.
Now, I send the user via links to https:// but when they leave the login-Area, they stay in https:// Mode.
I use now RewriteCond in standardconfiguration:
SetEnv APPLICATION_ENVIRONMENT show
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.xxxseitexxx.de$ [NC]
RewriteRule ^(.*)$ http://www.xxxseitexxx.de/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
RewriteRule !\.(js|ico|gif|jpg|jpeg|png|css|inc|pdf|csv|zip|rm|mp3|swf)$ index.php
Thanks in advance!
You can use this rule on top of other rules:
# force HTTP for not login or admininstration
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/admininstration|login/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
# force HTTPS for login or admininstration
RewriteCond %{HTTPS} off
RewriteRule ^(admininstration|login)/ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=302]
I'm trying to create multilingual shop having RewriteRules as follows:
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -l [NC]
RewriteRule .* - [L]
RewriteRule .*[^/]$ $0/ [R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com\$
RewriteRule ^(.*)$ $1?language=en [QSA]
RewriteRule ^catalog/product/([^/]*)/$ catalog/index.php?id=$1 [L,QSA]
RewriteRule ^catalog/([^/]*)/$ catalog/index.php?category=$1 [L,QSA]
Those rules don't work when trying to access http://example.com/catalog/product/111/
or http://example.com/catalog/category/
However when I delete
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com\$
RewriteRule ^(.*)$ $1?language=en [QSA]
everything works just fine.
What is the problem? How can I make this stuff work together?
You need to switch the order of the rules, and get rid of the L flag for some of them. This should work:
RewriteRule ^catalog/product/([^/]*)/ catalog/index.php?id=$1 [QSA,S=1]
RewriteRule ^catalog/([^/]*)/ catalog/index.php?category=$1 [QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)$ $1?language=en [QSA]