1

我不熟悉 .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 重写方面有一点经验!

最新的 .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/index.php 正在加载。URL 变为 createforweb.gr/blog/

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

4

1 回答 1

5

将此代码放在您.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]
于 2013-03-22T21:04:12.783 回答