简单 URL Mod_rewrite

发布于 2024-09-26 12:22:10 字数 318 浏览 3 评论 0原文

当调用此 URL 时,为什么以下 htaccess 文件会生成 300 错误?


URL: hxxp://subdomain.domainame.com/keyworda/

IndexIgnore *

RewriteEngine on
RewriteRule ^([a-z]+)/$ /index.php?p=$1


错误日志: 300 hxxp://subdomain.domainame.com/keyworda(无尾部斜杠)

Why does the following htaccess file generate 300 errors, when this URL is called?

URL: hxxp://subdomain.domainame.com/keyworda/

IndexIgnore *

RewriteEngine on
RewriteRule ^([a-z]+)/$ /index.php?p=$1

Error log: 300 hxxp://subdomain.domainame.com/keyworda (no trailing slash)

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

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

发布评论

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

评论(3

逆流 2024-10-03 12:22:10

编辑,我试图让我的答案简短,我并不是想让你准确地添加这一行。这只是为了显示缺失的斜线应该去哪里。

RewriteRule ^/([a-z]+)/$ /index.php?p=

我假设您正在 apache 配置目录中编辑 .conf 文件。 Chigley 的答案是假设您正在编辑每个目录的 .htaccess 配置文件。

编辑你的conf文件,如下所示:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_METHOD} ^TRACE
  RewriteRule .* - [F]
  RewriteRule ^/(test[a-z]+)/$ /index.php?id=$1 [L,R=301]
  ... your current stuff afterwords ...
</IfModule>
  • 我认为问题是你需要在yoru RewriteRule中使用初始的“/”。
  • 首先放置我的测试 RewriteRule 将确保它是匹配的。
  • 我添加了“test”前缀,因此该行不会干扰您网站的其余部分。这将使隔离该规则变得更加容易。因此,在测试时,除了具有 1 个或多个 az 之外,url 还必须以“/”结尾并以“test”开头才能匹配。
  • 要测试的 url 是: hxxp://...yourserver.../testkeyword/
  • 这应该将浏览器中的 url 更改为: hxxp://...yourserver.../index.php?id=
  • testkeyword您看到的 404 错误可能意味着:1) 没有规则匹配,并且没有与您的关键字匹配的文件。 2) 一条或多条规则匹配,但生成的 url 与您的 php 文件不匹配。

一旦它起作用,这就是在您的conf文件中使用的真正的RewriteRule:

RewriteRule ^/([a-z]+)/$ /index.php?id=$1 [L,R=301]

如果它不起作用,请编辑您的问题以将完整代码包含在
块,以及您的 httpd 版本,如下所示:

# httpd -v
Server version: Apache/1.3.33 (Darwin)
Server built:   Mar 20 2005 15:08:27

可能并不重要,但以防万一...

这是 mod_write 文档。最底部有一些例子。

Edit, I was trying to keep my answer short, I didn't mean for you to add this line exactly. It was just to show where the missing slash should go.

RewriteRule ^/([a-z]+)/$ /index.php?p=

I'm assuming you are editing a .conf file, in your apache config directory. Chigley's answer is assuming you are editing a per-directory .htaccess config file.

Edit your conf file to look like this:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_METHOD} ^TRACE
  RewriteRule .* - [F]
  RewriteRule ^/(test[a-z]+)/$ /index.php?id=$1 [L,R=301]
  ... your current stuff afterwords ...
</IfModule>
  • I think the problem is you need the initial '/' in yoru RewriteRule.
  • Putting my test RewriteRule first will make sure it is the one matching.
  • I added in the "test" prefix, so this line wont interfere with the rest of your website. This will make it easier to isolate this rule. So, when testing, the url must end with a "/" and begin with "test" for it to match, in addition to having 1 or more a-z.
  • The url to test is: hxxp://...yourserver.../testkeyword/
  • This should change the url in your browser to: hxxp://...yourserver.../index.php?id=testkeyword
  • The 404 error you are seeing can mean: 1) No rules match, and there is no file that matches your keyword. 2) One or more rules match, but the resulting url does not match your php files.

Once it's working, this is the real RewriteRule to use in your conf file:

RewriteRule ^/([a-z]+)/$ /index.php?id=$1 [L,R=301]

If it does not work, edit your question to include the full code in the
block, and your httpd version, like this:

# httpd -v
Server version: Apache/1.3.33 (Darwin)
Server built:   Mar 20 2005 15:08:27

Probably doesn't matter, but just incase...

Here is the mod_write documentation. There are some examples at the very bottom.

梦幻的心爱 2024-10-03 12:22:10

你能试试这个吗:

RewriteRule ^([^/]+)/?$ /index.php?p=$1

can you try this one:

RewriteRule ^([^/]+)/?$ /index.php?p=$1
夜光 2024-10-03 12:22:10

尝试

RewriteRule ^([a-z]+)/?$ /index.php?p=$1 [L]

Try

RewriteRule ^([a-z]+)/?$ /index.php?p=$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文