mod_rewrite help for php to html redirect, html to php intern, and support subdomains - .htaccess

I want to always show .html in the address bar. All .php requests to be redirected to a .html in the address bar. And .html requests to show the .php file.
I have set up my webhoster with following domain and subdomains
www.example.com => /
test1.example.com => /test1
test2.example.com = /test2
Then I setup .htaccess in the root directory as
RewriteEngine on
# Externally redirect .php requests to .html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /$1.html [R=302,L]
# Internally redirect .html requests to .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).htm$ $1.php [NC,QSA,L]
This works great
www.example.com => /index.php
www.example.com/abc/def/test.php => /abc/def/test.php - Address bar shows "www.example.com/abc/def/test.html"
www.example.com/abc/def/test.html => /abc/def/test.php
but not for the subdomain
test1.example.com => /test1/index.php
test1.example.com/abc/def/test.html => /test1/abc/def/test.php
(!!) test1.example.com/abc/def/test.php => 404 - Address bar shows "test1.example.com/test1/abc/def/test.html"
For some reason test1 shows double. What lines do I need to change/add in the htaccess?

Related

htaccess force https non-www in single redirect for all pages

I would like to redirect everybody to my https:// site (not https://www) and force HTTPS in single redirect. My relevant parts of .htaccess:
# Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
# Force HTTPS and remove WWW
RewriteRule ^(.*)/$ https://example.com/$1 [R=301,L]
The problem? It doesn't work for sub-pages!
While it does work for main page (eg. http://www.example.com redirects to https://example.com), it doesn't work for sub-pages (eg. http://www.example.com/contact sholud redirect me to https://example.com/contact but only www is removed and I end up on non-https site: http://example.com/contact).
How to change my .htaccess rules, to force HTTPS and remove WWW for my site and all pages of the site in single redirect?
After changes: it works for sub-pages, but in 2 redirects. Can it be done in only one?
Thanks to answer by Nisarg and some digging, my site now properly redirects to HTTPS even on sub-pages. Yay!
Relevant .htaccess code now looks like this:
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
# remaining htaccess mod_rewrite CODE for WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Unfortunately, it takes 2 steps to process. So, the question is still up: can it be done in 1 redirect?
Try the following:(Guessing you are using index.php)
RewriteEngine On
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
#
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule ^ index.php [L]
Make sure you clear your browser cache before testing this change.

mysterious exception in htaccess rewrites

My htaccess files contains only a few lines that firstly remove the www and then add ".php" to the slug to get the correct php file, so
www.kalicup.fr/seo
should rewrite to
kalicup.fr/seo
and then display the file seo.php (without the .php extension displaying in the url itself)
at the moment
kalicup.fr/seo
correctly displays seo.php without showing the file extension.
however, when I try
www.kalicup.fr/seo
it rewrites to
kalicup.fr/seo.php
adding the .php extension in the url
so there's abviously a problem in my htaccess but I can't see it !
here's my code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.fr)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.co\.uk)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
can anyone see the problem ?
Use that in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.(?:fr|co\.uk))$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Only one test for .fr and .co.uk.
And -MultiViews: http://httpd.apache.org/docs/2.2/en/mod/core.html#options
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
http://httpd.apache.org/docs/2.2/en/content-negotiation.html

How to change .htaccess file to redirect to www, not http?

Below is the code in my .htaccess file. How do I change it to redirect visitors to https://www and not http://.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Immediately after the RewriteEngine On directive, place the following:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
This will handle both the http to https and non-www to www redirection. Note that external redirections should generally come before internal rewrites (which is what you currently have).

301 Redirects and non-www to www Canonical Problems (Yii Framework)

The 301 redirection of non-www to www doesn't work completely on my site (CMS: Yii Framework).
Here's my htaccess:
IndexIgnore */*
RewriteEngine on
#if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
It's ok when I type example.com, the redirection points to www.example.com but when I type "example.com/tag/world" the redirection doesn't work. I don't know why. :/
And when I type example.com/news (category) I don't get the redirection to www.example.com/news, the URL redirects to example.com/index.php/.
In the urlManager section of the file main.php you can see below the code for the tag and the category:
'tag/<titre>'=>'frontend/categorie/tag',
'chroniqueur/<titre>'=>'frontend/categorie/liste',
'<titre>'=>array('frontend/categorie/index', 'urlSuffix'=>'/'),
'search/<titre>'=>'frontend/search/index',
Do I have to modify or change something ? I'm new on Yii.
Move 301 rule before other rule:
IndexIgnore */*
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Redirect all URLs to main domain htaccess

I would like to redirect all my URL's to www.domain.com
www.domain.com/site-1 must redirect to www.domain.com
www.domain.com/site-2 must redirect to www.domain.com
a.s.o
I tried:
RewriteEngine On
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
But when doing this I get redirected to: http://domain.com/index.php which is a a white page instead of the homepage
You can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . http://www.mydomain.co.uk/? [R=301,L]

Resources