HYDER ALI
HYDER ALI

Reputation: 86

URL rewrite not working as expected

I am using the following code in .htaccess file

DirectoryIndex home.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^$ - [L]
RewriteRule ^/?([^\./]*)[:;,\.]*$ $1.php
RewriteRule ^([a-zA-Z0-9_,-]+)/([0-9]*)$ listing.php?business=$2
  1. This code hides the .php extension of the PHP files in the root folder only, But I want to apply it to the PHP files in all subfolders.

  2. I also want to deny access using the original URL , that is the URL includes the .php extension.

  3. I also want the third rule should be worked.

Upvotes: 0

Views: 124

Answers (1)

anubhava
anubhava

Reputation: 785128

Replace your code with this:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^([a-zA-Z0-9_,-]+)/([0-9]+)$ listing.php?business=$2 [L,QSA]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s\?] [NC]
RewriteRule ^ - [F]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

Upvotes: 1

Related Questions