2013-03-22 92 views
1

我不熟悉.htaccess文件,所以如果我的问题有点荒谬,不好意思。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

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

这是一个可接受的解决方案,否则会在未来造成问题?

回答

4

将这个代码在你.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] 
+0

谢谢abubhava!尽管blog.php存在于网站目录中,但现在我收到以下错误消息:“在此服务器上未找到请求的URL /blog.php”。 – kapantzak 2013-03-22 21:17:24

+0

这很可能是因为其他的斜线规则。让我为你推荐一些东西。 – anubhava 2013-03-22 21:22:12

+0

立即尝试我编辑的代码。 – anubhava 2013-03-22 21:23:57