1453939
1453939

Reputation: 242

How to remove "/index.php?", ".php", and force trailing slash "/" in URLs? (htaccess rewrite rules)

we have a new site in a new directory in public_html as shown in below with yellow. We need to add rules to new_site/.htaccess to fulfill the following three rules:

  1. Removing .php from the end of URLs
  2. Force trailing slash / at the end of most urls (excluding .jpg, .png, ...)
  3. Removing /index.php? from the middle of various URLs.

enter image description here

Most probably, this would be what I tried to ask:

Currently,

domain.com/ns/abc.php
domain.com/ns/abc_abc.php
domain.com/ns/index.php?/abc
domain.com/ns/abc/index.php?/abc
domain.com/ns/abc/abc/index.php?/abc/123
domain.com/ns/abc/abc/index.php?/abc/abc/123/123

To

domain.com/ns/abc/
domain.com/ns/abc_abc/
domain.com/ns/abc/
domain.com/ns/abc/abc/
domain.com/ns/abc/abc/abc/123/
domain.com/ns/abc/abc/abc/abc/123/123/

We have also tried some previously posted rules to do this task. Such as

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /ns

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

The above code helped us and removed index.php. However, we could not figure it out how to remove /index.php?.

Any comment/advice is appreciated! Thanks!

Upvotes: 0

Views: 622

Answers (1)

Howli
Howli

Reputation: 12469

The following should allow you to achieve this:

RewriteEngine On
RewriteRule ^ns/(.*)/(.*)/index.php/(.*)/(.*)/(.*)/(.*)$ /ns/$1/$2/$3/$4/$5/$6/ [R,L]
RewriteRule ^ns/(.*)/(.*)/index.php/(.*)/(.*)$ /ns/$1/$2/$3/$4/ [R,L]
RewriteRule ^ns/(.*)/index.php/(.*)$ /ns/$1/$2/ [R,L]
RewriteRule ^ns/index.php/(.*)$ /ns/$1/ [R,L]
RewriteRule ^ns/(.*).php$ /ns/$1/ [R,L]

EDIT: Second try:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /ns/(.*)/(.*)/index\.php\?/(.*)\ HTTP
RewriteRule ^ /ns/%2/%3/%4\? [R,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /ns/(.*)/index\.php\?/(.*)\ HTTP
RewriteRule ^ /ns/%2/%3\? [R,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /ns/index\.php\?/(.*)\ HTTP
RewriteRule ^ /ns/%2\? [R,L]
RewriteRule ^ns/(.*).php$ /ns/$1/ [R,L]

Upvotes: 1

Related Questions