php - 使用 .htaccess 从 url 中删除文件名

标签 php .htaccess url web seo

我想从 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/

相关文章:

javascript - 数据表列过滤器未显示

php - 重写基于 php 的自定义电子商务网站的 URL

.htaccess - Silverstripe SEO URL - 重定向

url - 如何在 Apigee 中获取正确的代理 URL?

java - 从 URL 连接 Java 读取

php - 如何在 preg_match_all php 中获取组的值?

php - 尝试在我的 Mac 上安装 Composer,Brew 失败

php - jquery/WordPress : apply infinite scroll effect to new content loaded via AJAX

linux - 手动密码保护文件/文件夹 (Linux)

python - 检测缩短的目的地,或 "tiny"url