htaccess 正则表达式下划线和空格不起作用

发布于 2024-12-09 06:11:42 字数 897 浏览 1 评论 0原文

RewriteRule ^([A-Za-z0-9'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1 [NC]

我在这里使用这个正则表达式,它工作得很好,但是如果我举个例子:

RewriteRule ^([A-Za-z0-9 _'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1 [NC]

这里不起作用。由于空格和下划线,我想在正则表达式中包含空格和下划线,但它根本不起作用。我必须添加一些特别的东西吗?

这也不起作用:

RewriteRule ^(.*)$ public_html/$1 [NC]

我希望能够输入任何内容并在 public_html 文件夹中打开。

例如,我输入:www.mysite.com/site_1.php 然后它打开: www.mysite.com/public_html/site_1.php

这个 .htaccess 表达式正在使用 Apache 2.2.17 在 HostGator 服务器中进行测试 我也在我的本地主机 Apache 2.2.17 中进行了测试,并且发生了同样的情况。

添加 _ 和空格或 .* 时给出的错误是这样的:

内部服务器错误

服务器遇到内部错误或配置错误, 无法完成您的请求。

请联系服务器管理员admin@localhost并告知 错误发生时的信息,以及您可能做过的任何事情 这可能导致了错误。

有关此错误的更多信息可能会在服务器错误中提供 日志。

提前谢谢你们了。

RewriteRule ^([A-Za-z0-9'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1 [NC]

I'm using this Regex up here and it works just fine, but if I put for example:

RewriteRule ^([A-Za-z0-9 _'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1 [NC]

This up here doesn't work. because of the Space and underline, I want to include spaces and underline in the regexp, but it doesn't work at all. do I have to add something special to it ?

And this also doesn't work:

RewriteRule ^(.*)$ public_html/$1 [NC]

I want to be able to type anything and open in the public_html folder.

Ex, I type: www.mysite.com/site_1.php
then it opens: www.mysite.com/public_html/site_1.php

This .htaccess expression is being tested in the HostGator servers using Apache 2.2.17
and I also tested in my localhost Apache 2.2.17 as well, and same happens.

The Error given when adding the _ and space or .* is this:

Internal Server Error

The server encountered an internal error or misconfiguration and was
unable to complete your request.

Please contact the server administrator, admin@localhost and inform
them of the time the error occurred, and anything you might have done
that may have caused the error.

More information about this error may be available in the server error
log.

Thank you guys in advance.

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

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

发布评论

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

评论(1

回忆躺在深渊里 2024-12-16 06:11:42

来自 regular-expressions.info

字符类中唯一的特殊字符或元字符是右括号 (])、反斜杠 (\)、脱字号 (^)和连字符(<代码>-)。常见的元字符是字符类中的普通字符,不需要用反斜杠转义。


除了上述字符之外,您不应该转义字符集中的字符。这里的简单技巧是确保将连字符保留在字符类的最末尾。这将连字符标识为文字。

RewriteRule ^([a-zA-Z0-9'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ _/.-]*)$ public_html/$1 [NC]

为了避免“连续循环”,您可以在匹配规则之前包含此条件:

RewriteCond %{ENV:REDIRECT_STATUS} ^$

另外,如果您只是想匹配您也谈到的所有内容,我通常会执行如下操作。请注意,%{QUERY_STRING} 用于传递任何 GET 变量。如果您不打算使用常规 GET 变量,则可以将其删除。

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?request=$1&%{QUERY_STRING}

我希望这些信息对您有所帮助。

From regular-expressions.info:

The only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash.

You shouldn't be escaping characters within the character set other than the ones mentioned above. The simple trick here is to make sure you keep the hyphen at the VERY END of your character class. This identifies the hyphen as a literal.

RewriteRule ^([a-zA-Z0-9'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ _/.-]*)$ public_html/$1 [NC]

To avoid a "continuous loop" you could include this condition before your match rule:

RewriteCond %{ENV:REDIRECT_STATUS} ^$

Also, if you simply want to match everything like you also talked about, I usually do something like below. Note, the %{QUERY_STRING} is there to pass on any GET vars. You can remove that if you don't plan to use regular GET vars.

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?request=$1&%{QUERY_STRING}

I hope this information helps you out.

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