[英]SEO Friendly URL's with .htaccess
在發布此內容之前環顧四周,找不到與我的特定問題相匹配的任何內容。
在我的網站上,我更喜歡我的 URL 中沒有 .php,所以我在我的 .htaccess 文件中設置了它並且它完美地工作。 目前,我的網站上有一個名為“專輯”的頁面,這是目前年度最佳專輯。 但是,我也想在明年做,等等。 現在,我可以將其作為專輯?date=2017,但這看起來很難看。 我想將其作為 example.com/albums/2017 進行,因為它看起來更好,而且對 SEO 更友好。
我的 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?date=2017 在沒有重定向的情況下工作,但是當我放入重定向時,它會將您發送到 /albums/2017,但在頁面上沒有顯示任何內容。 如果我刪除“從文件名中刪除 PHP”重定向,它會在頁面上顯示信息。 很明顯,我已經確定該錯誤是由於文件的“從文件名中刪除 PHP”部分引起的,我只是不確定如何修復它。
我對 .htaccess 和 RewriteEngine 不是很了解,只知道足夠或非常基本的東西,如果這看起來很糟糕,請道歉。 我已經嘗試確保我沒有提出重復問題,但如果我有,抱歉,
像這樣試試,
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
弄清楚了。
# 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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.