RewriteRule in .htacces not working properly - .htaccess

In my .htaccess file I have several 301 redirects.
The following line works perfectly:
RewriteRule ^sub1/ https://example.com/sub1 [L,R=301]
However, if there is another / in de url, the page will not be redirected properly.
RewriteRule ^sub1/sub2/ https://www.example.com/sub [L,R=301]
This will end up at example.com/sub1/sub2/ although it loads the page of example.com/sub1 as it does nothing with the /sub2.
How can I force the correct url rewriting?
EDIT
I also have some lines to remove .php in the .htaccess that might be causing this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Related

.Htaccess redirect breaks file path

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]

301 dynamic redirects not correct working

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]

Why are clean URLs not working when redirecting to https?

I added some lines in my htacess file to redirect all http requests to https. Now when I type in a URL like: http://example.com/frames/view/4701362, it redirects to: https://example.com/index.php?q=frames/view/4701362. I can go to the page manually: https://example.com/frames/view/4701362 and the URL does not get changed.
I have Clean URLs set up
Htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
#some more stuff here, unrelated
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
Edit:
I tried reversing the Drupal index.php?q= line and the https lines like this...
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
But then my site did not load properly and said Page not found even at the homepage.
Thanks to #MikeRockett above, I was able to figure this out. In addition to his suggestion, I had to add [L,R=301] to the RewriteRule line.
<IfModule mod_rewrite.c>
RewriteEngine on
#some more stuff here, unrelated
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

.htaccess vanity urls and clean urls together

I've been able to set up .htaccess to enable vanity urls on my website. However, I'm encountering into a problem when I try to clean other urls on my website. I would give the scenario and my current .htaccess below:
Currently wwww.mywebsite.com/john redirects to profile.php?user=john. In profile.php I check to see user john exist in my database if false I redirect to a custom 404.php page. This work very well. The problem arises when I try to have wwww.mywebsite.com/photo/abcdefg be interpreted as www.mywebsite.com/photo.php?m=abcdefg. For some reason it believes photo is the name of the user and then rewrites it as if its a vanity url. Please see my .htaccess file code below:
ErrorDocument 404 http://www.mywebsite.com/404
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteRule ^([^\.]+)$ profile.php?username=$1 [L]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You need to do your redirect before any internal routing. Then, you need to explcitly match against /photo before you route to profile and change the .php -f:
ErrorDocument 404 http://www.mywebsite.com/404
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^photo/(.+)$ /photo.php?m=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /profile.php?username=$1 [L]

site loading for invalid URL's and redirection error

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]

Resources