如何使用 htaccess 将 URL 中的下划线替换为破折号?

use*_*331 2 php regex apache .htaccess mod-rewrite

我的 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]
Run Code Online (Sandbox Code Playgroud)

但这没有用。当我转到时出现 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>
Run Code Online (Sandbox Code Playgroud)

更新

@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');
    }
}
Run Code Online (Sandbox Code Playgroud)

display_errors = on当我转到 test-page/now-here 时,即使我通过 php.ini和我的 php 代码打开 php 错误,我也会得到一个空白的白页:

error_reporting(E_ALL);
ini_set('display_errors', 1);
Run Code Online (Sandbox Code Playgroud)

仍然只是白屏。

MrW*_*ite 8

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]
Run Code Online (Sandbox Code Playgroud)

您的规则顺序错误,因此实际上不会针对给定 URL 处理指令。但该规则中的正则表达式也不匹配示例 URL,因此无论如何也不会执行任何操作。

您使用的正则表达式将匹配/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]
Run Code Online (Sandbox Code Playgroud)

注意:首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。

我还想问为什么您只重定向(规范化)来自传递 +veUser-Agent字符串的用户代理的请求。(是的,它很可能是一个恶意机器人。)但是您仍然将此类请求传递给前端控制器而不是阻止它。(?) 我在上面的规则中保留了这个条件,只是简化了它,以防您有一些特定的要求。

您可能不想要<IfModule mod_rewrite.c>包装纸。有了这个<IfModule>指令,并且 mod_rewrite 突然不可用,那么您的网站无疑会以一种不明显的方式中断,并且您可能会收到大量 404 错误,这些错误可能会在一段时间内无法被接收。如果没有此<IfModule>指令,则站点将中断,并在服务器的错误日志中显示详细错误,这可能会触发警报/通知。有关详细信息,请参阅网站管理员 SE 上的以下问题:https://webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary

  • 我必须说,一个很好的答案 (2认同)