user2569919
user2569919

Reputation: 3

How to redirect a slash url to non slash url using htaccees

My url looks like

 localhost/test/page1.php

I would like to hide the php extension so i have used the below method to hide the php extension

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Now the url is

localhost/test/page1

But when i use the url like localhost/test/page1/ then it shows an error. HOw to redirect the slash url to non slash url

Upvotes: 0

Views: 58

Answers (2)

user1646111
user1646111

Reputation:

Test this:

RewriteRule ^([^/]+)/?$ $1.php [NC,L]

Upvotes: 1

Mike Brant
Mike Brant

Reputation: 71384

Just add a Rewrite Rule as shown below:

RewriteEngine on

# Remove slashes from incoming URI's that end with slashes.
RewriteRule ^/?(.+)/$ /$1 [R=301,L]

# Redirect redirect requests to PHP controllers
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.]+)$ /$1.php [NC,L]

This will redirect any requests not to web root that end in slash a slash-cleansed URL with a HTTP 301 header.

Upvotes: 0

Related Questions