2016-09-11 155 views
-1

發佈之前看看周圍,找不到任何符合我的具體問題。搜索引擎友好的URL與.htaccess

在我的網站上,我更喜歡讓我的網址沒有.php文件,所以我在.htaccess文件中設置了它,並且它完美地工作。目前,我在我的網站上有一個名爲「專輯」的頁面,這是當年的最佳專輯。不過,我還想在明年等這樣做。現在,我可以將它作爲專輯?日期= 2017年,但看起來很醜。我想將它作爲example.com/albums/2017來做,因爲它看起來好多了,而且更適合搜索引擎優化。

我在我的.htaccess如下:?

RewriteEngine On 

# Remove PHP from file names 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L] 

# Redirect albums.php?date=2017 to albums/2017 
RewriteCond %{THE_REQUEST} \s/albums\?date=([0-9]+)\s [NC] 
RewriteRule^/albums/%1? [R=301,L] 
RewriteRule ^albums/([0-9]+)$ /albums?date=$1 [L] 


Options -Indexes 

# Error Document Redirects 
ErrorDocument 403 http://example.com/errors/403 
ErrorDocument 404 http://example.com/errors/404 

albums.php日期= 2017年作品,未經重定向,但是當我把重定向的,它就會進入/專輯/ 2017年,但不會在頁面上顯示任何內容。如果我刪除「從文件名中刪除PHP」重定向,它確實會在頁面上顯示信息。所以很明顯,我已經發現錯誤是由於文件中的「從文件名中刪除PHP」部分引起的,我不知道如何解決它。

我對.htaccess和RewriteEngine知之甚少,只知道足夠或非常基本的東西,所以如果這看起來很可怕的話,我很抱歉。我當時一看,試圖確保我沒有做一個重複的問題,但對不起,如果我有,

+0

感謝誰低估了這一點,但沒有解釋爲什麼它被低估。 :-) –

回答

0

想通了。

# Redirect albums.php?date=2017 to albums/2017 
RewriteRule ^albums/([^/]*)$ /albums?date=$1 [L] 

# Remove PHP from file names 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L] 
0

嘗試像這樣,

RewriteEngine On 
# removed .php extension 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^([\w-]+)$ $1.php 

# you can directly use album/(date) in your url using this rule 
RewriteCond %{REQUEST_FILENAME}.php !-f 
RewriteCond %{REQUEST_FILENAME}.php !-d 
RewriteRule ^([\w-]+)/([\d]+)$ $1.php?date=$2 
+0

專輯?日期= 2017年的作品,但是當你去到相冊/ 2017年,它只是顯示404說該文件無法找到。 –

+0

好吧,我的意思是,當你打電話albums.php?date = 2017它是與PHP擴展或不工作 –

+0

album.php,只是一個正常的PHP頁面 –