.htaccess重写器规则重定向到同一域中的URL

w51jfk4q  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在尝试将用户重定向到特定的URL。我想创建.htaccess重写规则来完成此操作。
我喜欢这些:

https://example.com/career_by_education
https://example.com/international_careers
https://example.com/major_careers
https://example.com/career

转移到:

https://example.com/career_by_education/careers-by-educational-level.php
https://example.com/international_careers/international_careers.php
https://example.com/major_careers/academic_major_careers.php
https://example.com/career/career.php

我已经尝试了许多重写规则的变体来使其工作,并且如果我希望它重定向到不同的域,我是成功的。
例如,以下内容(.htaccss格式):

RewriteRule career_by_education   https://example2.com/career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers   https:/example2.com/international_careers/international_careers.php [L,R]
RewriteRule major_careers   https://example2.com/major_careers/academic_major_careers.php [L,R]
RewriteRule career   https://example2.com/career/career.php [L,R]

成功转移至(分别):

https://example2.com/career_by_education/careers-by-educational-level.php
https://example2.com/international_careers/international_careers.php
https://example2.com/major_careers/academic_major_careers.php
https://example2.com/career/career.php

但我希望重定向到相同的域(example.com),所以我尝试了以下操作(按此顺序):

RewriteRule career_by_education   https://example.com/career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers  https://example.com/international_careers/international_careers.php [L,R]
RewriteRule major_careers   https://example.com/major_careers/academic_major_careers.php [L,R]
RewriteRule career   https://example.com/career/career.php [L,R]

当我在浏览器中输入以下任何内容时:

https://example.com/career_by_education
https://example.com/international_careers
https://example.com/major_careers

我总是得到这个网址:

https://example.com/career/career.php

我尝试将RewriteRules的顺序反转为:

RewriteRule career   https://example.com/career/career.php [L,R]
RewriteRule major_careers   https://example.com/major_careers/academic_major_careers.php [L,R]
RewriteRule international_careers  https://example.com/international_careers/international_careers.php [L,R]
RewriteRule career_by_education   https://example.com/career_by_education/careers-by-educational-level.php [L,R]

但生成的URL始终为:

https://example.com/career/career.php

我还尝试删除替换的完整URL,如下所示:

RewriteRule career_by_education /career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers /international_careers/international_careers.php [L,R]
RewriteRule major_careers /major_careers/academic_major_careers.php [L,R]
RewriteRule career /career/career.php [L,R]

但结果总是重定向到以下URL:

https://example.com/career/career.php

我尝试了不同的表达方式,看看是否可行:

RewriteRule ^/career http://consul64.wwwaz1-ts107.a2hosted.com/career/career.php [L,R]
RewriteRule ^/major_careers http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]
RewriteRule ^/international_careers http://consul64.wwwaz1-ts107.a2hosted.com/international_careers/international_careers.php [L,R]
RewriteRule ^/career_by_education http://consul64.wwwaz1-ts107.a2hosted.com/career_by_education/careers-by-educational-level.php [L,R]

这些都导致了“403 Forbidden”错误-从命令行中,我确保我在根目录中有一个“index.php”,权限是正确的,如下所示:

chmod 644 ~/public_html/.htaccess
chmod 755 ~/public_html

然后我试了这些:

RewriteRule .*(?=major_careers) http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]
RewriteRule .*(?=international_careers) http://consul64.wwwaz1-ts107.a2hosted.com/international_careers/international_careers.php [L,R]
RewriteRule .*(?=career_by_education) http://consul64.wwwaz1-ts107.a2hosted.com/career_by_education/careers-by-educational-level.php [L,R]
RewriteRule  .*(?=career) http://consul64.wwwaz1-ts107.a2hosted.com/career/career.php [L,R]

生成的URL始终为:

https://example.com/career/career.php

我不明白为什么这种模式在重定向到外部URL时似乎有效,但在重定向到同一域上的URL时却不起作用。
有人能帮我带路吗?

8mmmxcuj

8mmmxcuj1#

RewriteRule career_by_education /career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers /international_careers/international_careers.php [L,R]
RewriteRule major_careers /major_careers/academic_major_careers.php [L,R]
RewriteRule career /career/career.php [L,R]

RewriteRule指令的第一个参数(* pattern *)是正则表达式。这不是"字符串精确匹配"。这些规则的问题在于 * pattern * 也匹配重定向的URL,因此会导致无休止的"重定向循环",因为"regex" career_by_education匹配URL路径中的字符串 * anywhere *。
你说它总是重定向到/career/career.php(上面的最后一条规则),但这在发布的规则中是不可能的--因为上面提到的redirect-loop。然而,如果你颠倒这些指令(你说你也尝试过),它就会发生,因为第一条规则(只匹配career)总是匹配--但同样,这是一个重定向循环。

  • 旁白:* 根据您的服务器配置,它可能不会触发重定向循环,例如,如果.php文件的请求被代理到CGI PHP引擎。

你需要更具体的正则表达式和匹配只规定的网址。即,你需要包括 * 锚 * 的正则表达式和匹配career_by_education * 只 *,而不是career_by_education * 任何地方 *。
例如:

RewriteRule ^career_by_education$ /career_by_education/careers-by-educational-level.php [L,R]
RewriteRule ^international_careers$ /international_careers/international_careers.php [L,R]
RewriteRule ^major_careers$ /major_careers/academic_major_careers.php [L,R]
RewriteRule ^career$ /career/career.php [L,R]

这些指令的顺序无关紧要。
如果您要重定向到同一个域,则不一定需要在 * substitution * 中使用方案+hostname(即绝对URL),但是,在域规范化的情况下,这有助于减少重定向的次数。
我不明白为什么重定向到外部URL时该模式似乎有效
因为在重定向之后不会再次处理这些指令(您现在位于不同的服务器上)。

RewriteRule .*(?=major_careers) http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]

使用非锚定正前视实际上与简单地major_careers相同。

RewriteRule ^/career http://consul64.wwwaz1-ts107.a2hosted.com/career/career.php [L,R]
RewriteRule ^/major_careers http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]
RewriteRule ^/international_careers http://consul64.wwwaz1-ts107.a2hosted.com/international_careers/international_careers.php [L,R]
RewriteRule ^/career_by_education http://consul64.wwwaz1-ts107.a2hosted.com/career_by_education/careers-by-educational-level.php [L,R]

这些都将导致"403 Forbidden"错误
由于斜杠前缀,这些规则永远不会匹配,因此它们实际上不会执行任何操作。

    • 更新日期:**

然而,这些重定向的一个"问题"是,你重定向的URL,例如/career_by_education,也是你重定向到的文件系统 * 目录 * 的名称。所以,默认情况下,mod_dir将尝试附加一个尾部斜杠,所以规则不匹配。403可能是由于该目录中缺少DirectoryIndex文档。严格地说,你的网址需要以一个斜杠结尾,或者在正则表达式中使斜杠可选(但这可能会导致双重重定向)。
例如:

# Trailing slash is mandatory
RewriteRule ^career_by_education/$ /career_by_education/careers-by-educational-level.php [L,R]

# Trailing slash is optional
RewriteRule ^career_by_education/?$ /career_by_education/careers-by-educational-level.php [L,R]

尽管URL最好首先不与目录的名称匹配。

相关问题