2011-11-30 69 views
1

我第一次使用htaccess爲我的網站html文件和1個php文件製作漂亮的網址。我只是想知道如果我能夠得到一些關於我的htaccess文件設置的建議,並且如果我設置它是一個好方法?因爲我寫的東西,我討厭我的網站在某些情況下不工作。 :(htaccess漂亮的網址設置

示例HTML文件:

before: http://www.domain.com/subdomain/htmlpage.html 
after: http://www.domain.com/subdomain/htmlpage/ 

單一的PHP文件:

before: http://www.domain.com/subdomain/phppage.php?p=1 
after: http://www.domain.com/subdomain/phppage/1/ 

我已經在規則中加入的index.html重定向到index.php我也不得不在每個文件的頭部添加「基本href」,因爲我們使用相對鏈接

htaccess的文件:

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^index\.html?$/[NC,R,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301] 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.+)/$ $1.html [L] 
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L] 
+0

是'htmlpage'現在實際上存在(存儲)作爲? 'htmlpage.html'(你只是想讓它看起來像一個目錄),或者是現在的新文件'htmlpage/index.html'? 當前設置中的困難 - AFAICT - 是您實際上正確更正了通過重定向和重寫使您看起來不同的URL。當舊的URL實際上不再有效時,事情會變得更加容易,並且您可以將它們重寫爲最終更好的URL。 –

+0

htmlpage存在並存儲在根(子域)目錄中 - 當您在/ htmlpage /中鍵入時,漂亮的url鏈接加載.htmlpage。我不確定如何使.html頁面無法正常工作......但如果訪問者已經爲書籤添加了一個.html頁面,那麼這會是一個壞主意嗎? – Jess

+0

這不是關於打破事情。但是,如果您基本上將「file.html」重寫爲「file」,然後讓您的web服務器自動重寫爲「file.html」,那麼當您惹起某些問題時,您可以輕鬆地結束重定向循環。 這就是爲什麼我問你的'文件'(在服務器上)是否叫'..dirs/somepage.html','... dirs/somepage/index.html'或其他什麼。獨立於您想要在其下進行服務的URL。 –

回答

1

此行

RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L] 

會發送一些網頁,以phppage.php即使他們不喜歡看

http://www.domain.com/subdomain/phppage/1/ 

因爲在第一個參數沒有phppage的提重寫規則。

+0

哦,是的,我可以看到它不會改變1,所以pages/2 /,/ 3 /,/ 4/etc都保持爲phppage/1/:(如果你不介意,我需要寫什麼來改變這個?:S – Jess

+1

RewriteRule^subdomain/phppage /([^/\.]+)/?$ phppage.php?p = $ 1 [L] –

+0

非常感謝!它現在可以正常工作!:D – Jess

1

嘗試添加在你的域的根目錄下,以您的.htaccess文件

Options +FollowSymLinks 

RewriteEngine On 
RewriteBase/

# redirect http://www.domain.com/subdomain/htmlpage.html to http://www.domain.com/subdomain/htmlpage/ 
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.html$ [NC] 
RewriteRule . %1/ [L,R=301] 


#redirect http://www.domain.com/subdomain/phppage.php?p=1 to http://www.domain.com/subdomain/phppage/1/ 
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.php$ [NC] 
# or alternatively if page is literally phppage uncomment below and comment above 
#RewriteCond %{REQUEST_URI} ^(/subdomain/phppage)\.php$ [NC] 
RewriteRule . %1/ [L,R=301] 

#if url does not end with/
RewriteCond %{REQUEST_URI} ^(/subdomain/.+[^/])$ [NC] 
# and its not for an existing file 
RewriteCond %{REQUEST_FILENAME} !-f 
# put one trailing slash 
RewriteRule . %1/ [L,R=301] 

#write http://www.domain.com/subdomain/htmlpage/ to htmlpage.html 
RewriteCond %{REQUEST_URI} ^/subdomain/([^/]+)/$ [NC] 
RewriteRule . %1.html [L] 


#write http://www.domain.com/subdomain/phppage/1/ to phppage.php?p=1 
RewriteCond %{REQUEST_URI} ^/subdomain/[^/]+/([0-9]+)/$ [NC] 
RewriteRule . phppage.php?p=%1 [L]