how to remove slash from url with htaccess - php

how to redirect trailing slash from URL i want remove slash from end and romove slash between FILENAME
url => http://localhost/
url => http://localhost/customer/
url => http://localhost/customer/account///login/
url => http://localhost/customer/account/login///register///
And after cleaning url
http://localhost
http://localhost/customer
http://localhost/customer/account/login
http://localhost/customer/account/login/register
this is code htaccess
RewriteEngine On
RewriteBase /
Options -Indexes
Options -MultiViews
# view only www
# RewriteCond %{HTTP_HOST} !^$
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteCond %{HTTPS}s ^on(s)|
# RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<Files ~ '^.ht'>
order allow,deny
deny from all
</Files>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# remove one slash
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)/+(/[^\s]+) [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L]
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

RewriteRule htaccess to always remove trailing slash even directory
Try To Use The Code Below -
DirectoryIndex index.php
RewriteEngine On
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)/+(/[^\s]+) [NC]
RewriteRule ^ %1%2 [R=302,L,NE]
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

Related

trailing slash from URL url for some php url only

I want to rewrite some URLs with .php to trailing slash URLs. However, I don't want to rewrite all URLs.
My code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$
RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
I want force slash on:
/about-us.php => /about-us/
/services.php => /services/
I don't need slash on:
/cart.php => /cart.php
/auth.php => /auth.php
You may use these rules in your site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(about-us|services)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to use only these rules in root .htaccess and test from a new browser or command line curl.
You can use this to remove the trailing slash from your /cart and /auth URIs .
Put the following two rules right below RewriteBase line in your htaccess :
#remove the trailing slash
RewriteRule ^(cart|auth)/$ $1 [L,R]
#rewrite /cart and /auth to their original location
RewriteRule ^(cart|auth)$ $1.php [L]

Blank Space / Whitespace Character for .htaccess

this rule is working for me but i have a problem
www.domain.com/description_functionhall/117/A%2520S%2520Garden
second rule is taking %2520 so i want that url in this format
www.domain.com/description_functionhall/117/A-S-Garden
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /function_hall_project/
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]
# To externally redirect /dir/foo.php?hall_id=123&name=abcd to /dir/foo/123/abcd
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&name=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&name=$3 [L,QSA]
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
You can insert this rule just below ## hide .php extension snippet line to convert all these white spaces to hyhen:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%2520|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]

Removing index.php from URLS with .htaccess

I have an index.php controller what all URLs that aren't existing files redirect to.
The .htaccess rules I currently have look like this:
RewriteEngine On
DirectorySlash Off
# Remove Trailing Slashes
RedirectMatch 302 ^(.*)/$ $1
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302]
# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
What I want to do is use a 301 redirect if index.php is included in the URL.
BUT! I can't use RewriteBase or queries that start with a slash because the webapp I'm developing could be in a subfolder. So basically, I only want to rewrite for the current directory:
example.com/foo/index.php/bar/whatever should redirect to example.com/foo/bar/whatever
example.com/foo/index/bar/whatever should also redirect to example.com/foo/bar/whatever
localhost/place/foo/index.php/bar/whatever should redirect to localhost/place/foo/bar/whatever
localhost/place/foo/index/bar/whatever should also redirect to localhost/place/foo/bar/whatever
How can I accomplish this with .htaccess?
UPDATED CODE:
Everything here works EXCEPT:
index.php is not removed from anywhere in the URL.
Here's the code:
<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]
# Reroute to index.php
#RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]
</IfModule>
Not a good idea to mix mod_rewrite rules with mod_alias ones. Try this updated .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php[\s?/] [NC]
RewriteRule ^(.*?)index\.php(/.*)?/?$ /$1$2 [L,R=301,NC,NE]
# Reroute to index.php
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]
</IfModule>
Make sure you have mod rewrite on on your server try
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
You can use like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

rewrite url after remove extension

I need your help.
I have a problem to rewrite a dynamic url after extension removed, I've rewritten this url but not works:
.htaccess like this:
<IfModule mod_rewrite.c>
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#add www 1
RewriteCond %{HTTP_HOST} !^www\.mondomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mondoamin.com/$1 [R=301,L]
#remove .php extension 2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# Removes index.php from ExpressionEngine URLs 3
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]
#rewrite url 4
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /article\.php\?id_ article =([^&\ ]+)([^\ ]*)
RewriteRule ^/article/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]
</IfModule>
results:
1+2+3 works fine but 4 not works
Thanks in advance
Try reordering the rules and keep 301 rules before internal rewrites:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#add www 1
RewriteCond %{HTTP_HOST} !^www\.mondomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mondoamin.com/$1 [R=301,L]
#rewrite url 4
RewriteCond %{THE_REQUEST} /article\.php\?id_article=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]
RewriteRule ^article/([^/]+)/?$ /article.php?id_article=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]
#remove .php extension 2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
# Removes index.php from ExpressionEngine URLs 3
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]
this is the my last .htaccess code:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
add www
RewriteCond %{HTTP_HOST} !^www.mondomain.fr$ [NC]
RewriteRule ^(.*)$ http://www.mondomaine.fr/$1 [R=301,L]
rewrite dynamic url
RewriteCond %{THE_REQUEST} /article.php\?id_article=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]
RewriteRule ^ article/([^/]+)/?$ /article.php?id_article=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)[^.#?\ ]+.php([#?][^\ ])?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+).php $1 [R=301,L]
remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.?)index.php/(.*) /$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php/$1 [L]

I want to redirect domain.com/index.php to domain.com

How to redirect mydomainname.com/index.php to mydomainname.com
Currently I use following codes also.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mydomianname\.com
RewriteRule (.*) http://mydomianname.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule>
This should be your complete .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mydomianname\.com
RewriteRule (.*) http://mydomianname.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule>
This example only works for http, but should roughly do the trick.
# Redirect requests with index.php to the corresponding request
# without the index.php
RewriteRule ^index.php(.*) http://mydomianname.com$1 [R=301,L]

Categories