.htaccess remove trailing slash - .htaccess

I have a htaccess file for a site to let it's script work . I don't know much about how it works, but I have to remove the trailing slash at the end.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
ErrorDocument 404 https://www.onlinegames.nl/
## 301 Redirects
# 301 Redirect 1
RewriteCond %{HTTP_HOST} ^www\.onlinegames\.nl$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTPS} =on
RewriteRule ^index\.html$ https://www.onlinegames.nl/? [R=301,NE,NC,L]
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
How can I remove the trailing slash at the end? I've tried to change a few things, but most of the times the script is not working correctly anymore and all the subpages are not working anymore.

You may use these rules in your site root .htaccess to replace your shown code. Note that ordering of these rules is also important and change in ErrorDocument.
ErrorDocument 404 /
RewriteEngine On
# remove www from host names
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# strip /index.html
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.html$ / [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]

Related

Remove trailing slash with htaccess with existing rules

I wish to remove a trailing slash when one is given using htaccess. What would be the best way to do it that will work with my existing rules as below:
# make sure www. is always there
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
A sample URL would be something like:
https://www.example.com/this-test/
I of course want the ending slash removed.
I've tried this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
But that does not work with the existing rules that are there. It ends up redirecting to the index.php pages due to the other rules.
Have it this way:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# if requested url does not exist pass it as path info to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [QSA,L]
Make sure to test it after completely clearing browser cache.

Removing only one of the 2 public values in the URL with htaccess

I have an e-commerce system written in Laravel 8. Public directory appears in URL.
The software's URL system is as in the network.
Homepage: website.com/public/Category: website.com/public/category
Product: website.com/public/category
CSS file URL address: website.com/**public**/themes/themename/**public**/css/style.css
JS File URL address: website.com/**public**/themes/themename/**public**/js/app.js
As you can see, there are 2 public values in CSS and JS files.
I updated the htaccess file as follows to remove the public value.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com
RewriteRule (.*) https://www.website.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
#RewriteRule ^ /%1%2? [R=301,L,NE]
#Remove index.php
#RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
#RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
#Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
#Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
#Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
However, it also removed the 2nd public value of CSS and JS files. That's why CSS and JS files fail.
How can I keep the 2nd public value constant and remove the first public value?
Have your htaccess file in following way, please make sure to clear your browser cache before testing your URLs. I have also corrected few flags in your other rules too.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.website.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
# remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/(themes/themename)/public(/css/app\.css|/js/app\.js)\s [NC]
RewriteRule ^ %1%2? [R=301,L]
RewriteRule ^(themes/themename)/(css/app\.css|/js/app\.js)/?$ $1/public/$2 [NC,L]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Rewritecond by `%{HTTP_HOST}` is ignored?

I have multiple domains that point to the same folder on my server. The code in index.php page recognizes which domain is accessing it and shows different content for each domain.
Now I want that www.domain-a.com\sitemap.xml opens /sitemap-a.xml so that each domain has its own sitemap.
I use the following rule in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
However, if I access www.domain-a.com\sitemap.xml then the content from the file /sitemap.xml is shown instead of the file sitemap-a.xml.
Is the redirecting rule wrong? If so, how can I fix it?
I am using Laravel. This is the full .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
# browser request: .php to non-php
# https://stackoverflow.com/questions/13222850/redirect-php-urls-to-urls-without-extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Can you try this rule just above # Handle Authorization Header line:
RewriteCond %{HTTP_HOST} (?:^|\.)domain-a\.com$ [NC]
RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC]

Removing the .php file extension from the URL Request

I am trying to beautify my URLs and want to get rid of the .php file extension. I've found a couple of other questions here such as this one (Removing the PHP file type extension) But none of the suggestions work for me. They send me to my 404.php or do not change the URL at all.
I figure the entire RewriteEngine code in my htaccess as a whole is conflicting in itself.
Here's what I got so far,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [R]
</IfModule>
I would like my domains to look like
https://www.example.com/about
Don't mix Redirect, RedirectMatch and RewriteRule directives in same .htaccess as they come from different Apache modules and their load sequence is unpredictable.
Have it this way:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to clear your browser cache before testing this change.
Put in .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
ErrorDocument 404 ./404
RewriteCond %{REQUEST_URI} ^404/$
RewriteRule ^(.*)$ ./404 [L]
Redirect 301 /index /
remove .php from the link :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

.htaccess remove index.php remove www. force https

I understand this is not necessarily an EE specific question, but it involves the classic EE removal of index.php from the URL as well as a couple more specific rewrites. It’s also a lazy question, as I’ve tried a few rewrite rule combinations and there are errors… so, what I’m after is (please).
Remove www.
Force https://
Remove index.php
The important note, is that on an (mt) Media Temple server when testing on subdomains such as dev.domain.com the good old multiple redirects problem is kicking in. Ideally there’s a way for one .htaccess file to rule them all. Many thanks in advance.
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Force https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
What worked for me (excluding the index.php part, only for removing WWW and forcing HTTPS), was this:
# Enable Rewrite Engine
RewriteEngine On
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Force https, specify http_host
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#-----the following part is untested, but should work to answer the full quesiton including the index.php part
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/js/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/css/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/fonts/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/themes/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/inc/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/admin.php$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/index.php?$1 [QSA,L]
</IfModule>

Resources