-1

从字面上看,我对此束手无策。我在 stackoverflow 上研究了许多其他问题和答案,但仍然找不到我需要的解决方案。我开始认为我想做的事情是不可能的。

所以……问题是这样的。我想打开以下内容:例如

www.mydomain.com/visa-information/country.php?country={COUNTRY-NAME}&passport={PASSPORT-NAME}

到以下漂亮的网址:

www.mydomain.com/visa-information/{COUNTRY-NAME}-visa-for-{PASSPORT-NAME}-citizens/

我的 htacces 文件中有一个部分成功的规则,如下所示:

RewriteRule ^/visa-information/([A-Za-z-]+)-visa-for-([A-Za-z-]+)-citizens/?$ visa-information/country.php?country=$1&passport=$2  [NC]

如果我在浏览器地址栏中输入网址,它可以正常工作并且可以满足我的要求,但我遇到的真正问题是通过我在几乎每个页面上都有的表单将它重定向到漂亮的网址地点。

我尝试了各种重定向规则,如下所示:

RewriteCond %{QUERY_STRING} country=([A-Za-z-]+)&passport=([A-Za-z-]+) [NC]
RewriteRule visa-information/country.php  visa-information/%1-visa-for-%2-citizens/? [R,NC,L]

但没有运气。我也尝试将 QSA 标志添加到上述重定向规则中,但它最终以无限循环结束。

我尝试使用 country.php 页面顶部的位置 php 重定向标头在表单提交后重新定向,如下所示:

if(isset($_GET['country']) && isset($_GET['passport'])) {
header("Location: " . $dir . "/visa-information/" . $currentCountry . "-visa-for-" . $currentPassport . "-citizens/");
exit();
}

我期待上面的工作就像直接在浏览器中输入漂亮的 url 一样工作,但事实并非如此,只是给了我一个 404 错误。

任何帮助是极大的赞赏。

谢谢

约达什

编辑

我的本地目录结构如下:

/webserver/mydomain.com/visa-information/etc...

在实时服务器上,它将是:

mydomain.com/visa-information/etc..

当我在本地机器上使用 Apache Alias 时,我将 RewriteBase 设置为:

RewriteBase /webserver/mydomain.com/

我目前有以下一组 RewriteRules 改编自 anubhava 给我的内容:

RewriteCond %{REQUEST_URI} visa-information/country.php     [NC]    
RewriteCond %{QUERY_STRING} country=([A-Za-z-]+)&passport=([A-Za-z-]+)  [NC]
RewriteRule visa-information/       visa-information/%1-visa-for-%2-citizens/? [R=301,L,QSA]

# internal redirect from pretty URL to old URL
RewriteRule ^visa-information/([A-Za-z-]+)-visa-for-([A-Za-z-]+)-citizens/?$    visa-information/country.php?country=$1&passport=$2 [NC,L]

目前,这给了我一个无休止的重定向循环,无论是在浏览器栏中输入漂亮的 url 时,还是使用我的表单时,但是如果我禁用前 3 条规则,那么我发现我可以将漂亮的 url 输入到地址栏和重写工作,但当然不是来自表单提交。

我真的不知道我做错了什么。为什么会有无限循环?

4

2 回答 2

0

Looks like the issue was actually with my php form and not the mod_Rewrite rules. The form "action" was pointing to the same form page (as in $SERVER['PHP_SELF']) which works fine without the rewrite rules, but causes an endless loop when they are activated.

I simply made a search_action.php page and then redirect the form there using a php header to the pretty url:

header("Location: " . $dir . "/visa-information/" . $currentCountry . "-visa-for-" . $currentPassport . "-citizens/");
exit();

The mod_rewrite rule I had originally works fine and now the user can get to the desired page with the pretty url from the form or by tyoing directly into the browser address bar.

I'm not sure it's actually possible to action a form to the same page whilst rewriting the query string, without using an intermediary action page from the form, or Javascript. I'm sure many of the more experienced programmers will have known this already, but not me unfortunately.

于 2013-10-07T13:20:48.513 回答
0

不确定第一条规则对您的工作方式,因为与 .RewriteRule中的前导斜杠不匹配.htaccess。此外,要将旧 url 重定向到漂亮的 URL,您需要使用THE_REQUEST表示 Apache 从浏览器接收到的原始请求的变量。用这个替换你的代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# external redirect from old URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+visa-information/country\.php\?country=([^\s&]+)&passport=([^\s&]+)\s [NC]
RewriteRule ^ /visa-information/%1-visa-for-%2-citizens/? [R=301,L]

# internal redirect from pretty URL to old URL
RewriteRule ^visa-information/([a-z-]+)-visa-for-([a-z-]+)-citizens/?$ /visa-information/country.php?country=$1&passport=$2 [NC,L,QSA]
于 2013-10-02T17:14:48.517 回答