mod_rewrite 来自域的基础

发布于 2024-12-09 16:46:56 字数 1171 浏览 1 评论 0原文

我不确定这是否可行,但我想像这样使用 mod_rewrite:

http://abc.com/something => http://abc.com/script.php?q=something

这不工作:

Options +FollowSymLinks
RewriteEngine on
RewriteRule /(.*)$ /script.php?q=$1

可以做吗?

编辑: 我应该提到,当我使用它时,mod_rewrite 工作正常:

Options +FollowSymLinks
RewriteEngine on
RewriteRule script/(.*)$ /script.php?q=$1

再次编辑: http://emailsms.me/redirect.php?id=abc

使用这个:

RewriteEngine on
RewriteRule ^([A-z]{1,})$ /redirect.php?q=$1

理论上http://emailsms.me/abc 应该输出 abc (它所做的只是回显当前的输入)。但我收到了 404 错误。

更新:似乎Options +FollowSymLinks 导致了这里的问题。如果我将其注释掉,我会收到 404 错误,但是当它在那里并且我输入任何内容(我的意思是任何内容,甚至是 #)时,我会在日志中收到此错误:

[Fri Oct 14 02:20:26 2011] [alert] [client 1.2.3.4] /home/me/redirects/.htaccess: Illegal option #

I'm not sure if this is possible but I want to use mod_rewrite like so:

http://abc.com/something => http://abc.com/script.php?q=something

This doesn't work:

Options +FollowSymLinks
RewriteEngine on
RewriteRule /(.*)$ /script.php?q=$1

Is it possible to do?

Edit:
I should mention mod_rewrite is working fine when I use this for example:

Options +FollowSymLinks
RewriteEngine on
RewriteRule script/(.*)$ /script.php?q=$1

Edit Again: http://emailsms.me/redirect.php?id=abc

Using this:

RewriteEngine on
RewriteRule ^([A-z]{1,})$ /redirect.php?q=$1

So theoretically http://emailsms.me/abc should be outputting abc (all it does is echo the input at the moment). but instead I get a 404 error.

Update: It seems as though Options +FollowSymLinks is causing the problem here. If I comment it out I get a 404 error but when its there and I put anything (I mean anything even a #) I get this error in my logs:

[Fri Oct 14 02:20:26 2011] [alert] [client 1.2.3.4] /home/me/redirects/.htaccess: Illegal option #

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

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

发布评论

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

评论(3

晌融 2024-12-16 16:46:56

试试这个:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /script.php?q=$1 [L,QSA]

您可能不希望重写规则中的尾随正斜杠,并且您需要检查请求的文件是否不存在(2个RewriteCond行),否则您将收到 500 服务器错误,因为重写循环(/script.php 将始终匹配 ^(.*)$ 并再次重写)。请注意,如果您没有 /script.php 文件,您将收到 500 错误,因为重写将循环。

Try this:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /script.php?q=$1 [L,QSA]

You probably don't want the trailing forward slash in your rewrite rule, and you need to check that the requested file doesn't exist (the 2 RewriteCond lines) otherwise you'll get a 500 Server error because the rewrite loops (/script.php will ALWAYS match ^(.*)$ and get rewritten again). Note that if you don't have a /script.php file, you'll get a 500 error because the rewrite will loop.

稚然 2024-12-16 16:46:56

听起来您是从 .htaccess 而不是虚拟主机配置执行此操作。在这种情况下,您必须丢失 RewriteRule 中的第一个 / 。

It sounds like you're doing this from .htaccess, rather than vhost configuration. In that case you must lose the first / in your RewriteRule.

罗罗贝儿 2024-12-16 16:46:56

您可以尝试类似的操作:

RewriteEngine on
RewriteRule ^([A-z]{1,})$ /script.php?q=$1

如您所见,它会因 (.*) 崩溃

You could try something like:

RewriteEngine on
RewriteRule ^([A-z]{1,})$ /script.php?q=$1

It crashes with the (.*) as you can see

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文