【问题标题】:htaccess rewrite rule - no extension and slashhtaccess 重写规则 - 没有扩展名和斜杠
【发布时间】:2013-03-12 19:17:34
【问题描述】:

我不熟悉 .htaccess 文件,如果我的问题有点荒谬,请见谅。

当我要求时

localhost/mydomain/blog.php

我想要一个这种形式的网址:

localhost/mydomain/blog/

我的网站目录如下所示:

.htaccess 文件包括以下内容:

删除 .php 扩展名

<IfModule mod_rewrite.c>        
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
</IfModule>

通过这个链接

<a href="blog">Blog</a>

网址是:

localhost/createforweb_1.1/blog

但我想添加一个斜杠,所以如果我在 htaccess 中添加这些行:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/createforweb_1.1/$1/ [L,R=301]

我收到此错误:

The requested URL /blog/.php was not found on this server.

我肯定很简单但是我对.htaccess rewrite有点经验!

最新的 .htaccess 文件(整个文件)

<IfModule mod_rewrite.c>
 Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
 RewriteEngine On
# RewriteBase /
</IfModule>

Options -MultiViews

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

替代解决方案

我刚刚为每个页面创建了一个文件夹,将文件移到其中并将它们重命名为 index.php。新的目录树如下所示:

现在我请求 blog 文件夹并且 blog/index.php 正在加载。 URL 变为 createforweb.gr/blog/

这是一个可接受的解决方案,还是会在未来产生问题?

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    把这段代码放在你的.htaccessDOCUMENT_ROOT目录下:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    ## Adding a trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule . %{REQUEST_URI}/ [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ /$1.php [L]
    

    【讨论】:

    • 谢谢阿布哈瓦!虽然 blog.php 存在于网站目录中,但我现在收到以下错误消息:“在此服务器上找不到请求的 URL /blog.php。”
    • 这很可能是因为其他尾部斜杠规则。让我也为你推荐一些东西。
    • 还是同样的错误!我尝试了很多组合,但我不知道我到底在做什么..
    • 在您的问题中发布您最新的 .htaccess。我也不知道createforweb_1.1 是什么?帽子是你的 DOCUMENT_ROOT 吗?
    • 我已经用新的 .htaccess 编辑了我的问题。 createforweb_1.1 是我的 DOCUMENT_ROOT。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2012-01-21
    • 2011-11-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多