Martney Acha Martney Acha - 2 months ago 10
PHP Question

URL .htaccess in php

Do anyone can give some advice on how to do

URL for my php page

I have this URL
website.com/profile.php/8

What I need is this URL

website.com/profile/8

What I do is

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]


But it / "Backslash" is reading for another directory, any idea how to fix this ?

Answer

You can use these rules:

RewriteEngine On

# Forward /profile/8 to /profile.php/8
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php/$2 [L]

# Forward /profile to /profile.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond is to ensure corresponding .php exists.

Comments