我的 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]
但它沒有用。轉到時出現 404 錯誤/test-page/now-here
我究竟做錯了什么?
這是我的完整版.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,但是它不會轉到我的測驗頁控制器中的 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');
}
}
當我轉到 test-page/now-here 時,我得到的只是一個空白頁,即使我通過 php.inidisplay_errors = on和我的 php 代碼打開了 php 錯誤:
error_reporting(E_ALL);
ini_set('display_errors', 1);
仍然只是一個白屏。
uj5u.com熱心網友回復:
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,所以無論如何都不會做任何事情。
您使用的正則運算式將 match /test-page/something/now_here,而不是/test-page/now_here您的示例。根據您的示例,.*/之后test-page/是額外的。
您現有的(看起來像)HTTP 到 HTTPS 和非 www 到 www 規范重定向也位于錯誤的位置(它需要在重寫到前端控制器之前進行,否則最終會重定向到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]
NB: Test with a 302 (temporary) redirect first to avoid potential caching issues.
I would also question why you are only redirecting (canonicalising) requests from user-agents that pass a ve User-Agent string. (Yes, it's quite possibly a malicious bot.) Yet you are still passing such requests to your front-controller and not blocking it. (?) I've left this condition in the above rule, just simplified it, in case you have some specific requirements.
You probably do not want the <IfModule mod_rewrite.c> wrapper. With this <IfModule> directive in place and mod_rewrite was suddenly not available then your site will no doubt break in a non-obvious way and you'll likely get a ton of 404s, which may not be picked up for a while. Without this <IfModule> directive then the site will break with a detailed error in the server's error log which should potentially trigger an alert/notification. See the following question on Webmasters SE for more info on this: https://webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439371.html
