2013-03-22 98 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