Ad

Remove Filename From Url With .htaccess

- 1 answer

I want to remove file name and extension from url, i have a directory (Blogs) and two files in it (Main_blogs.php) and (Blog.php)

localhost/SikandarIqbal.net/blogs/

when i open Main_blog.php in directory "Blogs" i get the above url which i was able to get with this htaccess file :

DirectorySlash Off
DirectoryIndex disabled
RewriteEngine on
# prevent direct access to PHP files
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]
# rewrite requests for a directory to index.php
RewriteCond %{REQUEST_FILENAME}/Main_blogs.php -f
RewriteRule ^.*$ $0/Main_blogs.php [L]

and when i open a specific blog from Main_blog.php the url becomes

localhost/Sikandariqbal.net/Blogs/Blog.php/My-Blog-name

what i really want is to be like something

localhost/Sikandariqbal.net/Blogs/My-Blog-name


Remove the filename Blog.php between /Blogs/ and /My-Blog-name

please help!
thanks

Ad

Answer

You need an additional rule for this:

DirectorySlash Off
DirectoryIndex disabled
RewriteEngine on

# prevent direct access to PHP files
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Blogs)/page/(\d+)?$ $1/?pn=$2 [L,NC,QSA]

# rewrite requests for a Blogs/Blog.php/<url>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Blogs)/([\w-]+)/?$ $1/Blog.php/$2 [L,NC]

# rewrite requests for a directory to Main_blogs.php
RewriteCond %{REQUEST_FILENAME}/Main_blogs.php -f
RewriteRule ^.*$ $0/Main_blogs.php [L]
Ad
source: stackoverflow.com
Ad