.htacces 捕获页面名称并将其作为 get 方法发送

发布于 2024-10-30 22:11:41 字数 267 浏览 1 评论 0原文

我正在尝试捕获一个网址,例如

http://www.mysite.com/somepage.php?sometext=somevalue

并将其重定向到。

http://www.mysite.com/index.php?page=somepage.php&sometext=somevalue

我尝试在网上搜索这样的.htaccess,但找不到。

你能帮我吗?

I am trying to capture a url such as

http://www.mysite.com/somepage.php?sometext=somevalue

and redirect it to.

http://www.mysite.com/index.php?page=somepage.php&sometext=somevalue

I tried searching for such .htaccess online, but couldn't find it.

Can you please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-11-06 22:11:41

我很确定这是重复的,但我在找到它/它们时遇到了一些问题 [编辑:我找到了一个,尽管可能不是最好的例子]。

无论如何,这是一个用相当标准的代码解决的相当标准的问题:

RewriteRule ^(.*)$ index.php?get=$1 [L,QSA] 

RewriteRule 将整个请求捕获为 $1,并将其作为 页面传递到 index.php 获取参数。

末尾的 [QSA] 标志表示采用任何现有的 GET 参数(示例中的 sometext=somevalue),并将它们添加为新请求中的附加 GET 参数。 ([L] 标志只是表示这应该是执行的最后一条规则。)

请注意,这还将重定向对图像或 CSS 文件等内容的请求,因此最好添加以下行 直接在此规则之前

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

这些行表示“如果请求是针对实际存在的文件或目录,则不处理该规则。”这样,对真实文件的请求将直接由 Apache 提供服务,而不是由 PHP 脚本处理(或更可能是错误处理)。

I'm quite sure this is a duplicate, but I'm having a bit of an issue finding it/them [Edit: I found one, though possibly not the best example].

Anyway, this is a fairly standard problem resolved with fairly standard code:

RewriteRule ^(.*)$ index.php?get=$1 [L,QSA] 

The RewriteRule captures the entire request as $1, and passes it to index.php as the page GET parameter.

The [QSA] flag on the end says to take any existing GET parameters (sometext=somevalue in your example), and add them as additional GET parameters on the new request. (The [L] flag just says that this should be the last rule executed.)

Note that this will also redirect requests for things like images or CSS files, so it's good to add the following lines directly before this rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

These lines say "if the request is for a file or directory that actually exists, don't process the rule." That way, requests for real files will be served directly by Apache, rather than being handled (or more likely, mishandled) by your PHP script.

陌上青苔 2024-11-06 22:11:41
RewriteRule ^(.*).php?sometext=(.*)$ index.php?page=$1.php&sometext=$2 [QSA,L] #rewrite
RewriteRule ^(.*).php?sometext=(.*)$ http://www.mysite.com/index.php?page=$1.php&sometext=$2 [R=301,L] #redirect
RewriteRule ^(.*).php?sometext=(.*)$ index.php?page=$1.php&sometext=$2 [QSA,L] #rewrite
RewriteRule ^(.*).php?sometext=(.*)$ http://www.mysite.com/index.php?page=$1.php&sometext=$2 [R=301,L] #redirect
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文