htaccess : access non www domain is showing index.php in url - .htaccess

I've tried every configuration possible but I just cant make it working.
I know the topic has come up number of times but I can't get the right solution for my case though it should be pretty simple.
When accessing www.example.fr, everything works fine and I get redirected to https://www.example.fr.
Now if I access example.fr then I get redirected to https://www.example.fr/index.php.I also just noticed that if I access https://www.example.com (same domain, different extension that belongs to me) I get redirected to https://www.example.com/index.php
Here is my .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L,QSA]
RewriteCond %{HTTP_HOST} ^example.fr [NC]
RewriteRule ^(.*)$ http://www.example.fr/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
I want to get rid of that /index.php forever.
Any help would be appreciated !

You must place redirects before rewrites:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /web/
RewriteCond %{HTTP_HOST} ^example\.fr [NC]
RewriteRule ^ https://www.example.fr%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L,QSA]
</IfModule>

Related

.htaccess redirecting to index.php

I am having an issue with my .htaccess file. I am using Slim framework and have a number of routes set up that are working fine.
But when you type the webaddress without www. it is redirected to index.php
Example:
https://www.example.com/Route --- Works
www.example.com/Route --- Works
https://example.com/Route redirects to https://www.example.com/index.php
example.com/Route redirects to https://www.example.com/index.php
.htaccress File:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
I am pretty sure it is an issue with the .htaccess file but just cant figure it out.
Any help would be great or pointers.

.htaccess force www and force https for certain path and Concrete5 pretty URLs

edit: This question is now moot as the whole site will be served using HTTPS
My .htaccess is causing a redirect loop.
I need all three sections to work, the purpose of each is in the comment.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Force HTTPS for /book unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTPS} !^on$
RewriteRule ^book https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Force www prefix unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{REQUEST_PROTOCOL}://www.%{HTTP_HOST}/$1 [L,R=301]
# Concrete5 pretty URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I'm having to work on the live server because the certificate is only valid there (with www. required).
I've tried many variations of the above, but am stumped, so am hoping fresh eyes on this will help, many thanks in advance.
Your 2nd rule isn't looking right as REQUEST_PROTOCOL has value of HTTP/1.1.
You can use this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Force HTTPS for /book unless dev or already there
RewriteCond %{HTTP_HOST} !^dev\.
RewriteCond %{HTTPS} !^on$
RewriteRule ^book https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# Force www prefix unless dev or already there
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} !^(?:www|dev)\. [NC]
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
# Concrete5 pretty URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

Can't access permalink without www in front

I have a plugin to set up permalinks on my site however it only works when lead by www in front of the address. All other site addresses function fine with our without the leading www. I've been playing around my htaccess file but can't figure out how to fix this. The first portion is generated by the plugin, while I inserted the last 2 lines. Any suggestions on how to fix this? The last condition on my file seems to be getting ignored by all the permalinks.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^permalinks_dispatcher\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /cms/plugins/permalinks_dispatcher.php [L]
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
</IfModule>
You only have to begin with your www-rule first
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^permalinks_dispatcher\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /cms/plugins/permalinks_dispatcher.php [L]
</IfModule>

.htaccess retrieve from folder if subdomain, not 301 redirecting?

I want my site to just point to /user folder if the request is a subdomain.
If the request is subdomain.site.com/admin, then the site should show the page for subdomain.site.com/user/admin.
The problem with my code is that it makes an 301 redirect instead of just keeping the url-address.
My code look like this:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se(.*)
RewriteCond %{HTTP_HOST} ^[^.]+.bildsida.se(.*)
RewriteRule ^$ user/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) user/$1 [L]
</IfModule>
And you can try for yourself, go to http://mickes.bildsida.se/admin and see how the address changes to /user/admin. Very disturbing...
You just need a few of the lines you showed to get this working.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} ^.+\.bildsida\.se
RewriteRule ^(.*)$ user/$1 [L]
I adjusted the HTTP_HOST checks because that parameter only looks at the domain name, so you don't need the (.*) at the end. I also removed the checks that look if the file exists or is a directory, since you want everything redirected (no reason to make it possible to access files from other subdomains, for example)
You code can be simplified to these lines : (try them instead)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?bildsida\.se [NC]
RewriteRule ^(.*)$ /user/$1 [QSA,L]
Finally! After days and nights of reading forums and documentations, and testing all possible ways, I got it to work!
Sulotion:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} !^bildsida.se
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ user/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? user/$1/ [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.+)/ user/$1 [QSA]
</IfModule>

Rewrite with htaccess not working

i have a site wich have a dynamic url for 3 pages only. and for that i dont go for php function. So I decided to go for .htaccess rewrite rules but I am not having any luck yet.
This is my actual url: /index.php?mode=service&inid=1
I want to rewrite it to this: /home-theater
I try it my self and with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com.au$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com.au/$1 [L,R=301]
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.mysite.com.au/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^home-theater/?$ index.php?mode=service&inid=1 [L,NC,QSA]
I still can't get it to work.
Also I write index.php to www redirect before this code, so I guess if it cause any issue or not. index.php to www site redirects work perfectly but this is not working.
Use L flag like this:
RewriteRule ^home-theater/?$ index.php?mode=service&inid=1 [L,NC,QSA]
Remember this will not change the URL in the browser since this will internally forward request to your index.php.
Also if this doesn't work then provide your full .htaccess in your question.
Update: I have modified your .htaccess here, please try it out now after clearing your browser cache:
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.com.au$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com.au/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^ http://www.mysite.com.au/ [R=301,L]
RewriteRule ^home-theater/?$ index.php?mode=service&inid=1 [L,NC,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]

Resources