大网数据广告活动
网站运营与故障问答

Execution order of different htaccess redirects

· 52CE 我爱测

It seems that no matter what I search I just can't understand why I keep getting this issue and I for some reason can't find a good explanation on how it works. Also sorry if this quest was asked before but I have been searching …

问题原文

原帖链接 · Controvi · CC BY-SA 3.0

It seems that no matter what I search I just can't understand why I keep getting this issue and I for some reason can't find a good explanation on how it works. Also sorry if this quest was asked before but I have been searching for an answer for 2 days and just can't find the answer. (probably searching the wrong keywords)

BUT.

I am currently having an issue regarding my redirects that use the Redirect and RewriteRule functions.

The context is as follows.

The company I work for has recently migrated and updated its website from server and design. This of course means there are pages that are not available anymore or URLs that need to be redirected.

The issue first came up when I was walking through all the redirects and noticed that some redirects didn't go to the page they should.

Below is a piece of the the .htaccess file that is giving me the issues. The lines are in the same order as they are in my .htaccess.

<IfModule mod_rewrite.c>
RewriteEngine On

Redirect 301 /nieuws/168/Luchtvaartmaatschappij-delft-onderspit-bij-Hoge-Raad [https domainname]/nieuws
Redirect 301 /nieuws/145/Artikel-Telegraaf-Vertraging [https domainname]/nieuws
Redirect 301 /nieuws/145/Artikel-Telegraaf:-Vertraging.html [https domainname]/nieuws
Redirect 301 /nieuws/108/Wie-gaat-nu-via-(reis)verzekeraars-werken-`Reisagenten-kunnen-klanten-ook-verwijzen` [https domainname]/blog/euclaim-gaat-nu-via-reis-verzekeraars-werken
Redirect 301 /nieuws/136/Technisch-mankement-geen-overmacht.html [https domainname]/blog/technisch-mankement-geen-overmacht

RewriteRule ^nieuws/([1-9]|[1-9][0-9]|[1-9][0-9][0-9]||[1-9][0-9][0-9][0-9])/.*$ /nieuws [R=301,NC,L]

The problem is when I go to /nieuws/136/Technisch-mankement-geen-overmacht.html I get redirected to /nieuws.

The issue

Now the issue isn't that I don't know what part of the htaccess is causing this but more why and how can I solve this.

The RewriteRule is picking up the URL and redirecting to /nieuws and it doesn't matter where I place it as it just keeps picking it up without fail. I tried putting the RewriteRule at the top, bottom, right behind the redirects like in my example but nothing seems to stop the RewriteRule from taking the top priority.

Is this normal behaviour or am I doing something wrong here?

If you need any more info please let me know.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

Redirect belongs to mod_alias, whereas RewriteRule is a mod_rewrite directive. Two different modules. Different modules execute independently and at different times during the request.

Unless you have a customised Apache server then mod_rewrite always executes before mod_alias, despite the apparent order of the directives in the Apache config file (eg. .htaccess).

Generally, if you are using mod_rewrite for redirecting (and/or URL-rewriting) then you should use mod_rewrite for all your redirects to avoid such conflicts.

Note that you will need to make sure any intermediary caches have been cleared before testing since 301s are cached hard by the browser.


Redirect 301 /nieuws/168/Luchtvaartmaatschappij-delft-onderspit-bij-Hoge-Raad [https domainname]/nieuws

As an example, since Redirect is prefix matching, the equivalent RewriteRule directive would be:

RewriteRule ^nieuws/168/Luchtvaartmaatschappij-delft-onderspit-bij-Hoge-Raad(.*) https://example.com/nieuws$1 [R=301,L]

However, if this is intended to be an exact match then:

RewriteRule ^nieuws/168/Luchtvaartmaatschappij-delft-onderspit-bij-Hoge-Raad$ https://example.com/nieuws [R=301,L]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。