.htaccess - 使用 URL 重写时出现问题(相对路径不起作用)

标签 .htaccess mod-rewrite url-rewriting relative-path absolute-path

我正在使用 .htccess 文件来重写我的 URL。我在我的 .htaccess 文件中使用了以下规则

Options +FollowSymlinks
RewriteEngine on 
RewriteBase /
RewriteRule ^product/([0-9]+)/([A-Za-z0-9+]+)$ /product.php?productid=$1&prodname=$2

重写工作正常。

但是当我尝试通过相对路径访问任何其他页面时,在所有情况下,URL 都没有正确重定向。

像下面的例子

<form action "something.php" method="post">
   <input type="text" name="1" />
   <input type="submit" value="submit" />
</form>

当我点击提交时,加载的页面是 http://mydomain/product/1/something/something.php,它不存在,因此抛出 404 错误

我尝试在 head 中使用 base 标签,但这会在我使用页面 anchor 和模式窗口时产生问题。它被重定向到索引页面。

如果需要更多详细信息,请告诉我。

我对此很陌生。那么,有人可以帮我解决这个问题吗?

提前致谢, 卡尔提克

最佳答案

这是一个 URL 解析问题。参见 mod_rewrite URL info required了解详情。

您可以使用绝对 URL 路径或绝对 URL:

<form action="/something.php" method="post">
<form action="http://example.com/something.php" method="post">

或者您更改基本 URL(请参阅 BASE HTML element)以便从该 URL 而不是当前 URL 解析每个相对 URL:

<base href="/">
<base href="http://example.com/">

关于.htaccess - 使用 URL 重写时出现问题(相对路径不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/938997/

相关文章:

.htaccess - HTACCESS 重写的 URL 正在重定向查询参数

apache - 使用 mod_rewrite 将 301 发送到 SERVER_NAME

php - 使用 htaccess 从 URL 中删除文件夹名称

java - 具有 Angular URL 重写功能的嵌入式 Jetty

php - 删除文件扩展名 htaccess 不起作用

php - Laravel:如何将路由缓存 5 分钟?

apache - htaccess 404 停止工作

apache - mod_rewrite 用于友好的 URL

apache - 将旧子域重定向到新子域

asp.net - ASP.NET MVC 提供的 URL