【问题标题】:Remove Filename from url with .htaccess使用 .htaccess 从 url 中删除文件名
【发布时间】:2016-08-14 07:35:12
【问题描述】:

我想从 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 打开一个特定的博客时,网址变成了

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

我真正想要的是成为某种东西

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


删除 /Blogs/ 和 /My-Blog-name 之间的文件名 Blog.php

请帮忙!
谢谢

【问题讨论】:

    标签: php .htaccess url web seo


    【解决方案1】:

    你需要一个额外的规则:

    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]
    

    【讨论】:

    • 它不工作 找不到错误对象!在此服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。
    • 如果我手动更改 url 并将其调整为 htaccess 中的设置,它可以正常工作,但是当我通过锚打开时,它会自动添加文件名,现在这是锚 Open 需要进行哪些更改才能正常工作。
    • 如果我添加诸如 mysite/blogs/my-blog-name 之类的 url,你给我的 htaccess 工作正常......但使用锚它不会
    • 现在 url 变成 localhost/sikandariqbal.net/Blogs/Blogs/blog-name 并且不会工作..
    • 我用过这个 GO 我不知道它现在是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 2016-04-13
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2015-06-23
    • 2015-07-27
    相关资源
    最近更新 更多