htaccess can not remove index.php - .htaccess

I used this code to force www and remove index.php:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It works on centOS server. but not in Ubuntu 16.
It can force www, but it does not remove index.php.
Also I want to redirect example.org to example.com.

There's no good reason why it would part work on one OS and not the other. Except if previous (erroneous) redirects had been cached.
However, your directives are in the wrong order. The internal rewrites (front controller) needs to come after the canoncial redirects, otherwise it will never "force www" or "remove index.php" when requesting a "virtual URL".
For example:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
# Front controller should be after canonical redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
also I want redirect example.org to example.com
Add the following before the above redirects:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R,L]
You will need to clear your browser cache before testing.

Related

Redirecting www to non-www, http to https and domain.com to domain.com/index.php

Am trying to use .htaccess to redirect:
1) all www to non-www e.g. www.example.com/page.php to example.com/page.php
2) all http to https e.g. http://example.com/page.php to https://example.com/page.php
3) the domain (and subfolders) to their index.php page e.g. https://example.com to https://example.com/index.php and https://example.com/en-gb/ to https://example.com/en-gb/index.php
I can get 1) and 2) working correctly with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
But I am struggling with including 3) as well. I have tried this but no luck:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
Update - tried this but no luck
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
Redirect / https://example.com/index.php
Second update - tried this but same as before I get index.php added recursively to the URL:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://example.com/index.php$1
Clear your browser cache and put the following code at your main directory .htaccess file , no need to go every folder and put code there :
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://yoursite.com/$1 [L,R=302]
# the two lines above will catch both http://www & http:// and force https://
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*) https://yoursite.com/$1 [L,R=302]
# the three lines above will catch only https://www and redirect it to none wwww
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^ /index.php [R=302,L,NE]
# the two lines above will force index.php to domain when request to index.php comes without it
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ /$1/index.php [R=302,L,NE]
# the two lines above will force index.php to any directory when request to index.php comes without it
Test it , if it's oK , change every 302 to 301 to get permanent redirection
Well - thanks for your help everyone :)
I eventually got there. In case this helps anyone else with the same problem... this went in the root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^$ https://example.com/index.php
And this in each folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}
RewriteCond %{REQUESTFILENAME} !-f
RewriteRule ^$ https://example.com/en-gb/index.php

Redirect all domains to use HTTPS

I have this code in my .htaccess file which does as the comments say:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
Options -Indexes
# redirect all traffic to correct domain
RewriteCond %{HTTP_HOST} ^itl\.|^(www\.)?(integratelecom|integra|integratelecommunications)\b [NC]
RewriteRule ^ https://www.example.net%{REQUEST_URI} [L,R=301,NE]
# redirect admin./ssladmin. sub domain to the correct folder
RewriteCond %{HTTP_HOST} (admin|ssladmin)\.itl\.uk\.net$ [NC]
RewriteRule !^admin/system/ admin/system%{REQUEST_URI} [L,NC]
# redirect subdomain.X to subdomain.example.net
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(?!itl\.)[^.]+\. [NC]
RewriteRule ^ https://%1.example.net%{REQUEST_URI} [L,NE,R=302]
# map subdomains to correct folder
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.net$ [NC]
RewriteRule !^subdomains/ subdomains/%1%{REQUEST_URI} [L,NC]
RewriteRule ^(subdomains/|admin/|index\.php$) - [L,NC]
# stop external linking to website but allow listed domains below
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(.*\.)?itl.uk.net [NC]
#RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.|admin\.|ssladmin\.)?example.net [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
#######################################
############## MAIN SITE ##############
#######################################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]
RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
All of the above is working fine, but i need to force every domain and subdomain to use HTTPS, I have tried adding in:
RewriteCond %{HTTPS} off %HTTP_HOST [NC]
RewriteRule ^ https://%1.domain.net%{REQUEST_URI} [L,NE,R=302]
But that keeps returning an Internal Server Error
I also tried these two lines, but that adds %25 to the end of the URL
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
To force HTTPs you can use:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
It basically says, if HTTPs is not equal to ON, then it will force the website to display using SSL.
Make sure you clear your cache before testing this.

Mod_rewrite http->https redirect EXCEPT one URL

OK, so I'm trying to do this:
!www -> www
http -> https
EXCEPT
/[a-f0-9]{11}/
https://www.domain.com/[a-f0-9]{11}/ -> http://www.domain.com/[a-f0-9]{11}/
map to controller: index.php?(uri)
e.g. /controller/action/id -> index.php/controller/action/id
Summary: all URLs should be https://www.domain.com/(.*) except /[a-f0-9]{11}/, which should be forced to http only.
I have an old set of rules (see below) which don't look very clean.
I've tried adding to them to account for /[a-f0-9]{11} and it ends up redirecting instead of remapping, so I end up with www.domain.com/index.php/thepattern0
How do I clean these rules up and make this work?
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
#redirect to www
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
#redirect to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#hide index.php
RedirectMatch 404 .*php\.ini
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
You can use:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
#redirect to www
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]
#redirect to http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]
#hide index.php
RewriteRule \.(?:php|ini)$ [L,R=404,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Force https:// for some URLs, and force www. on all

I'm trying to set up an SSL for a site using Codeigniter.
I need to to force https:// on certain pages (such as login, basket etc).
What I have so far, which forces www. successfully, and removes the index.php from the URL for cleaner results with Codeigniter:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Keep your redirect rules before rewrite ones:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{THE_REQUEST} /(login|basket|shop)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Hide .php extension in ALL subdirectories with .htaccess

I am trying to hide the .php extensions on my page for all subdirectories with .htaccess.
Currently I have
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It works perfectly fine with normal pages, however any page inside a folder gives me a 500 Internal Service Error. An example URL would be
http://www.motorcityline.com/portfolio/michigantinting
Which is actually /portfolio/michigantinting.php. The .php file does exist, I just don't have the .htaccess file configured properly.
Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# add www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Try this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.php [L]

Resources