htaccess重写规则 - 没有扩展名和斜杠

时间:2013-03-22 19:47:48

标签: .htaccess mod-rewrite url-rewriting

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

当我要求

localhost/mydomain/blog.php

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

localhost/mydomain/blog/

我的网站目录如下:

enter image description here

.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。新目录树如下所示:

enter image description here

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

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

1 个答案:

答案 0 :(得分:5)

将此代码放在.htaccess目录下的DOCUMENT_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]
相关问题