我想从 url 中删除文件名和扩展名,我有一个目录 (Blogs) 和其中的两个文件 (Main_blogs.php) 和 (Blog.php)
localhost/SikandarIqbal.net/blogs/
当我在“博客”目录中打开 Main_blog.php 时,我得到了上面的 url,我可以通过这个 htaccess 文件获得它:
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]
当我从 Main_blog.php 打开一个特定的博客时,url 变成了
localhost/Sikandariqbal.net/Blogs/Blog.php/My-Blog-name
我真正想要的是成为某种东西
localhost/Sikandariqbal.net/Blogs/My-Blog-name
删除/Blogs/和/My-Blog-name 之间的文件名 Blog.php
请帮忙!
谢谢
最佳答案
为此你需要一个额外的规则:
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]
关于php - 使用 .htaccess 从 url 中删除文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940125/