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.
Related
I am trying to write my own MVC platform in PHP for a school project and facing a problem with my .htaccess file. After following several tutorials and reading answers here on Stackoverflow I am still failing at getting it right. Briefly, I am rewriting requests to the root domain to index.php and then calling my router to get the controller and so on, everything is working fine except for one thing, sometimes and for no apparent reason I type a URL like mydomain.com/page and it changes to mydomain.com/index.php?/page
here is the code I have, a colleague has the same code and it is working!
any help will be appreciated. thanks
DirectoryIndex /index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
AddHandler application/x-httpd-php73 .php .php5 .php4 .php3
if i type the www. before the domain name it always goes to /index.php?/
It seems to me that the first RewriteRule gets applied which rewrites the ${REQUEST_URI} variable and then the second RewriteRule applies which performs a redirect on the already rewritten ${REQUEST_URI}. As RewriteCond only ever apply to one RewriteRule you can check the www. and http first:
DirectoryIndex /index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
AddHandler application/x-httpd-php73 .php .php5 .php4 .php3
This will redirect you first before actually rewriting the URL
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>
I have a problem with redirecting single webpages within my website.
I am trying to redirect https://job-center.hu/munkaero_kozvetites/ to https://job-center.hu/munkaero-kozvetites/ and I am using the following htaccess file to do this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?q=/$1 [QSA]
</IfModule>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.allasajanlat.job-center.hu [NC]
RewriteRule ^(.*)$ https://allasajanlat.job-center.hu/$1 [L,R=301]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.job-center.hu [NC]
RewriteRule ^(.*)$ https://job-center.hu/$1 [L,R=301]
Options -Indexes
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Redirect 301 /munkaero_kozvetites/ https://job-center.hu/munkaero-kozvetites/
#SetEnv skip-cache
The result is a strange URL and I am not sure if this is OK.
Would this be considered as duplicate content by Google or this is OK as it is.
Thanks for your help in advance.
For your question about google, Redirect 301 is a permanently redirect, which means google will consider both sites the same content.
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>
.htacess error
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.netsentries.com
RewriteRule ^(.*)$ http://netsentries/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Error only comes when I add this line.
RewriteCond %{HTTP_HOST} ^www.netsentries.com
RewriteRule ^(.*)$ http://netsentries/$1 [R=301,L]
I have to add it because my site is not loading with www now. I want to make it load.
Use this code and your htaccess problem will be solved:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# no www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
and if you have problem loading your site with www it is because of loosing www CNAME record from your website's host. You should check the nameservers from "DNS zone editor" of your websites hosting package and adding required records for www like this:
NAME:www.yoursite.com. TTL:14400 TYPE:CNAME RECORD:yoursite.com.
this will cause your website to work in both with and without www.
Try this once
RewriteCond %{HTTP_HOST} ^www\.netsentries\.com
RewriteRule ^(.*)$ http://netsentries.com/$1 [R=301,L]