.htaccess url重写没有传递参数 - 是否有某个地方的apache设置阻止了这个?

时间:2022-02-13 10:51:50

Okay, so this problem has completely stumped me and the other devs I work with. Here is the rundown:

好吧,所以这个问题让我和其他与我合作的开发者感到困惑。这是破旧:

I have a local dev environment setup with Mac Apache2 pointed at /Users/myusername/Sites/

我有一个本地开发环境设置与Mac Apache2指向/ Users / myusername / Sites /

Within /Sites I have two folders, /site-1 and /site-2, both of which have virtual hosts pointed at them site-1.dev & site-2.dev. Both site-1 and site-2 are running local installs of PerchCMS.

在/ Sites内我有两个文件夹,/ site-1和/ site-2,它们都有虚拟主机指向site-1.dev和site-2.dev。 site-1和site-2都运行PerchCMS的本地安装。

Within /site-2 I have an .htaccess file which I am trying to set up a URL rewrite that takes the URL /detail/slug-here and translates it into /detail.php?s=slug-here

在/ site-2中我有一个.htaccess文件,我试图建立一个URL重写,它接受URL / detail / slug- here并将其翻译成/detail.php?s=slug-here

I have tried the following rewrites (at the suggestion of PerchCMS support) and both have failed to pass the s param:

我尝试了以下重写(在PerchCMS支持的建议下)并且都未能通过s参数:

RewriteRule ^detail/([a-zA-Z0-9-/]+)$ detail.php?s=$1 [L]
RewriteRule ^site-2/detail/([a-zA-Z0-9-/]+)$ /site-2/detail.php?s=$1 [L]

Additional info:

  1. Yes mod_rewrite is enabled in apache... in the same .htaccess file it totally works if I do a simple rewrite like this...

    是的,在apache中启用mod_rewrite ...在同一个.htaccess文件中,如果我像这样进行简单的重写,它完全有效...

    RewriteRule dangerzone.html index.php
    
  2. One odd behavior that I've noticed is that if I remove everything from .htaccess I can still pull up detail.php by pointing my browser at /detail/test-item-1...(yes I have restarted my server) so its behaving as if there is still some sort of rewrite in place and loading detail.php sans param just as it continues to do with the rewrite in place - is this a clue that there is something off somewhere else in my server config? Note, RewriteRule dangerzone.html index.php does NOT work once it is removed from .htaccess.

    我注意到的一个奇怪的行为是,如果我从.htaccess删除所有东西,我仍然可以通过将我的浏览器指向/detail/test-item-1 ...(我已经重新启动我的服务器)来提取detail.php所以它表现得好像仍然存在某种重写并加载detail.php sans param就像它继续处理重写一样 - 这是一个线索,我的服务器配置中的其他地方有什么东西吗?注意,RewriteRule dangerzone.html index.php从.htaccess中删除后不起作用。

1 个解决方案

#1


Have this code in your site root .htaccess (inside /site-2/):

在您的站点root .htaccess(inside / site-2 /)中包含此代码:

Options -MultiViews
RewriteEngine On

RewriteRule ^detail/([a-zA-Z0-9/-]+)/?$ detail.php?s=$1 [L,QSA,NC]

Important is to turn off MultiViews options here. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

重要的是在此处关闭MultiViews选项。选项MultiViews由Apache的内容协商模块使用,该模块在mod_rewrite之前运行,并使Apache服务器匹配文件的扩展名。所以/ file可以在URL中,但它将提供/file.php。

#1


Have this code in your site root .htaccess (inside /site-2/):

在您的站点root .htaccess(inside / site-2 /)中包含此代码:

Options -MultiViews
RewriteEngine On

RewriteRule ^detail/([a-zA-Z0-9/-]+)/?$ detail.php?s=$1 [L,QSA,NC]

Important is to turn off MultiViews options here. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

重要的是在此处关闭MultiViews选项。选项MultiViews由Apache的内容协商模块使用,该模块在mod_rewrite之前运行,并使Apache服务器匹配文件的扩展名。所以/ file可以在URL中,但它将提供/file.php。