[英]How to replace underscores with dashes in URL using htaccess?
我有 URL 看起來像這個example.com/test-page/now_here
,但我希望 URL 看起來像example.com/test-page/now-here
。
我在我的.htaccess
文件中嘗試了以下內容:
RewriteRule ^(/?test-page/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/?test-page/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
但它沒有用。 當我轉到/test-page/now-here
時出現 404 錯誤
我究竟做錯了什么?
這是我的完整.htaccess
:
Options -Indexes
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^example\.com$
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteRule ^(/?test-page/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/?test-page/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
<IfModule mod_headers.c>
<If "%{REQUEST_SCHEME} == 'https' || %{HTTP:X-Forwarded-Proto} == 'https'">
Header always set Strict-Transport-Security "max-age=31536000"
</If>
</IfModule>
更新
@MrWhite 解決方案確實有效,現在當我轉到 url test-page/now_here 時,它會轉到 test-page/now-here,但是它不會轉到我的測試頁 controller 中的 php 方法,這是完整代碼:
class TestPage_Controller extends App_Controller {
function index() {
$this->smarty->assign('currentPage', 'testpage');
$this->smarty->assign('pageTitle', 'Test Page');
$this->smarty->assign('description', "This is a test page.");
$this->smarty->display(MDCHAT_THEME . '/page_meta.tpl');
$this->smarty->display(MDCHAT_THEME . '/page_header.tpl');
$this->smarty->display(MDCHAT_THEME . '/test_page.tpl');
$this->smarty->display(MDCHAT_THEME . '/page_footer.tpl');
}
function now_here() {
$this->smarty->assign('currentPage', 'testpage');
$this->smarty->assign('pageTitle', 'Test Page');
$this->smarty->assign('description', "This is a test page.");
$this->smarty->display(MDCHAT_THEME . '/page_meta.tpl');
$this->smarty->display(MDCHAT_THEME . '/page_header.tpl');
$this->smarty->display(MDCHAT_THEME . '/here.tpl');
$this->smarty->display(MDCHAT_THEME . '/page_footer.tpl');
}
}
當我轉到測試頁面/現在-這里時,我得到的只是一個空白頁面,即使我通過 php.ini display_errors = on
和我的 php 代碼打開了 php 錯誤:
error_reporting(E_ALL);
ini_set('display_errors', 1);
仍然只是一個白屏。
RewriteEngine On RewriteCond %{REQUEST_FILENAME}.-f RewriteCond %{REQUEST_FILENAME}.-d RewriteRule ^(.*)$ index.php/$1 [L] RewriteCond %{HTTPS}.=on RewriteCond %{HTTP_USER_AGENT} ^(:+)$ RewriteCond %{SERVER_NAME} ^example\.com$ RewriteRule,* https?//www.%{SERVER_NAME}%{REQUEST_URI} [R=301?L] RewriteRule ^(/?test-page/?*/[^/]*.)_([^/]*?_[^/]*)$ $1-$2 [N] RewriteRule ^(/?test-page/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]
您的規則順序錯誤,因此實際上不會為給定的 URL 處理指令。但是該規則中的正則表達式也不匹配示例 URL,因此無論如何都不會執行任何操作。
您使用的正則表達式將匹配/test-page/something/now_here
,而不是您示例中的/test-page/now_here
。 test-page/
之后的.*/
根據您的示例是額外的。
您現有的(看起來像)HTTP 到 HTTPS 和非 www 到 www 規范重定向也位於錯誤的位置(在重寫到前端控制器之前需要 go,否則它將最終重定向到index.php
) ,但它也無法規范化http://www.example.com/
(HTTP + www)和https://example.com/
(HTTPS + 非 www)。 如所寫,它僅規范化http://example.com/
(HTTP + 非 www)。
請嘗試以下操作:
RewriteEngine On
# 1.0 - Replace underscores with hyphens in last path segment
RewriteRule ^(test-page/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
# 1.1 - Redirect to remove the final underscore
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)\.?$ [NC]
RewriteRule ^(test-page/[^/]*?)_([^/_]*)$ https://www.%1/$1-$2 [R=301,L]
# 2 - non-www to www AND HTTP to HTTPS
RewriteCond %{HTTP_USER_AGENT} .
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)\.?$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# 3 - Rewrite to front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
注意:首先使用 302(臨時)重定向進行測試以避免潛在的緩存問題。
我還會質疑為什么您只重定向(規范化)來自傳遞 +ve User-Agent
字符串的用戶代理的請求。 (是的,它很可能是一個惡意機器人。)然而你仍然將這樣的請求傳遞給你的前端控制器而不是阻止它。 (?) 我在上面的規則中留下了這個條件,只是簡化了它,以防你有一些特定的要求。
您可能不想要<IfModule mod_rewrite.c>
包裝器。 有了這個<IfModule>
指令並且 mod_rewrite 突然不可用,那么你的網站無疑會以一種不明顯的方式中斷,你可能會得到大量的 404,這可能有一段時間不會被拾取。 如果沒有此<IfModule>
指令,則站點將因服務器錯誤日志中的詳細錯誤而中斷,這可能會觸發警報/通知。 有關更多信息,請參閱網站管理員 SE 上的以下問題: https://webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.