This is the htaccess for my website currently:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(article/?).*$
RewriteRule ^article/?(.*)$ article.php?$1 [L, QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(info/?).*$
RewriteRule ^info/?(.*)$ info.php?$1 [L, QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(admin/?).*$
RewriteRule ^admin/?(.*)$ admin.php?$1 [L, QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L, QSA]
# Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301, L]
# Force to WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301, L]
If the cache is cleared on the browser and I attempt to load "rotak.ro/article/rain", it redirects to "https://rotak.ro/article/rain", and I therefore fail to load external php css and java files because of the missing www.
If instead I load "rotak.ro", it correctly adds the www to the start of the url.
I'm assuming the RewriteCond for the WWW is failing to confirm in the cases where there are multiple slashes in the url, but I don't understand what's going on wrongly.
I'd like every url written, so rotak.ro/article/rain or rotak.ro/info/cookies to redirect to https://wwww.rotak.ro/ ....
How should I go about fixing this issue?
Have your htaccess rules file in following manner. You need to change order of redirect and www implementation rules. Then we need not to have multiple rules for your internal rewrite that could be handled within a single rules set itself.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
#Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)/?$ https://%{HTTP_HOST}/$1 [R = 301, L, NE]
#Force to WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)/?$ https://www.%{HTTP_HOST}/$1 [L, NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(article|info|admin)/?(.*)/?$ $1.php?$2 [NC,L, QSA]
NOTE: When we are putting redirection 301 in www rules it was affecting internal rules, so we had to remove it off. Even for OP internal rewrites were NOT working with OP's attempts.
Related
I'm using an htaccess rewrite rule to redirect subdirectory values as a variable:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/members/*
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+[^/])(.*)$ http://example.com/index.php?username=$1 [P,L]
This works great and uses the P flag so the URL displayed in the browser doesn't redirect. However, when I add the code to force the domain to run via https...
Options -Indexes
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/members/*
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+[^/])(.*)$ http://example.com/index.php?username=$1 [P,L]
...this causes the URL in the browser to change. For example:
https://example.com/myvariable
redirects in the browser address bar to:
https://example.com/index.php?username=myvariable
It does successfully force the SSL, but how can I accomplish both at the same time without the URL changing?
After a ton more testing I was able to determine that removing the domain from the rewrite rule as well as the [P] flag and it works:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/members/*
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.+[^/])(.*)$ /index.php?username=$1 [L]
I'm attempting to canonicalization my URL. However, when adding the relevant code, it breaks my other rewrite rules which allows me to hide the .html/ .php. It works fine when landing on the homepage but when you go to a page with www. and a file path at the end of the URL the page redirects to the homepage without www.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^www\.bespoke-apertures\.co.uk$ [NC]
RewriteRule (.*) https://bespoke-apertures.co.uk/$1 [R=301,L]
Have it like this in different order:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(bespoke-apertures\.co.uk)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]
Make sure to test this in a new browser to avoid old cache.
Try this to redirect without www :
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
I need to change old dynamic urls to new, so I have created .htaccess file, but something wrong and redirect to ERR_TOO_MANY_REDIRECTS problem.
Below my .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/(.+)/$ /cat.php?name=$1 [QSA,L]
RewriteCond %{QUERY_STRING} ^name=(.*)$ [NC]
RewriteRule ^cat\.php$ /movie/%1/? [R=301,L]
You need to check the original URI, and redirect to the pretty URL from that:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /cat\.php\?name=([^\s&]+) [NC]
RewriteRule ^cat\.php$ /movie/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/(.+)/$ /cat.php?name=$1 [QSA,L]
In my MVC application, I'm directing all of my requests to my index.php file using an .htaccess file. In addition, I'd like to enforce SSL. Using this post, I've added '#enforce https' code . However, when I add the code to my .htaccess file, my browser complains that it will be unable to complete the request. How can I have both rewrite rules in the same file?
RewriteEngine On
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
#enforce https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The rule that rewrites to index.php has a regex: ^(.+)$, which matches everything. That includes anything you want to redirect. You need to have your redirect rules before your routing rules. Additionally, you need to make sure the nothing in index.php will redirect the browser back to non-SSL:
RewriteEngine On
#enforce https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
currently I am using this htaccess access,but the site still loads for invalid url, it doesn't goto 404.
ex: http://www.couponcoder.in/babyoye.com -->works and main content
http://www.couponcoder.in/babyo ----> invalid url, it displays homepage
and site loads for both slash and without slash, I just want it to redirect non-slash
www.couponcoder.in/babyoye.com/
www.couponcoder.in/babyoye.com
can someone help out with this?
Options +Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?couponcoder\.in$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1couponcoder.in%{REQUEST_URI} [R=301,L]
RewriteRule ^admin$ Admin/index.php?qstr=$1 [L]
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]
I can't read if you want to have the non-slash version or if you want to redirect the non-slash version to the slashed one. I'm assuming you want the former.
...
#remove slash, and redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]