[英]how to stop htaccess rewrite rule from rewriting all urls
i am having a mod rewrite problem and i am new to htaccess not because i tried to do a mod_rewrite and link i try to open assumes that i am in profile.php我有一个 mod 重写问题,我是 htaccess 的新手,不是因为我试图做一个 mod_rewrite 并且我尝试打开的链接假设我在 profile.php
heres my code这是我的代码.htaccess
file contents .htaccess
文件内容
ErrorDocument 404 /404,/
RewriteEngine On
RewriteRule ^([a-z\A-Z\0-9\_\-]+)$ profile.php?username=$1
RewriteRule ^([a-z\A-Z\0-9\_\-]+)/$ profile.php?username=$1
htdocs(xampp)
|
=======================================================================================
| | | | | |
login, 404, style.css profile.php index.php .htacccess
htdocs(xampp) | ======================================================================================= | | | | | | login, 404, style.css profile.php index.php.htacccess
RewriteRule ^([az\AZ\0-9\_\-]+)$ profile.php?username=$1
The problem is with the regex - you shouldn't be backslash escaping the literal 0
inside the character class.问题在于正则表达式 - 你不应该是反斜杠 escaping 字符 class 内的文字0
。 In doing so you are increasing the character range (from "char code 0" instead of "char code 48" - a literal zero) and so includes the dot ( .
- character code 46).在这样做时,您正在增加字符范围(从“char code 0”而不是“char code 48” - 文字零),因此包括点( .
- 字符代码 46)。 This results in profile.php?username=<something>
(and any file) getting further rewritten resulting in $_GET['username']
having the value profile.php
- as you've stated.这导致profile.php?username=<something>
(和任何文件)被进一步重写,导致$_GET['username']
具有值profile.php
- 正如你所说。
Remove all the backslash escapes...删除所有反斜杠转义...
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [L]
Providing the hyphen ( -
) is at the end (or beginning) of the character class, it does not need to be escaped.如果连字符 ( -
) 位于字符 class 的末尾(或开头),则不需要对其进行转义。
However, this still won't work for login
and 404
URLs (unless you append a comma to the filename).但是,这仍然不适用于login
和404
URL(除非您 append 文件名的逗号)。 Although this highlights another potential problem.尽管这凸显了另一个潜在问题。 These represent physical files so should have a file extension.这些代表物理文件,因此应该具有文件扩展名。 By omitting the file extension, Apache won't necessarily know how to handle/process it.通过省略文件扩展名,Apache 不一定知道如何处理/处理它。 By default, it will simply be sent back to the client - unprocessed - without a Content-Type
header and then left for the browser to interpret - probably as text/html, but could be text/plain (HTML is exposed to the client), depending on the browser.默认情况下,它将被简单地发送回客户端 - 未处理 - 没有Content-Type
header 然后留给浏览器解释 - 可能是 text/html,但也可以是 text/plain(HTML 向客户端公开) ,取决于浏览器。
If login
is required to be processed as PHP then it should be called login.php
.如果login
需要被处理为 PHP 那么它应该被称为login.php
。 If you want to be able to access it from the client as /login
(no extension) then you need an additional rewrite directive in .htaccess
, just as you did for /qwerty
.如果您希望能够以/login
(无扩展名)的身份从客户端访问它,那么您需要在.htaccess
中添加一个额外的重写指令,就像您对/qwerty
所做的那样。 This directive needs to go before your existing directive in order to avoid conflicts.该指令需要在您现有指令之前go 以避免冲突。 (You can't have a username called "login" unless you change the format of your /qwerty
URL to identify this as a user.) (除非您更改/qwerty
URL 的格式以将其识别为用户,否则您不能拥有名为“login”的用户名。)
404
is an error document and shouldn't be directly accessible anyway. 404
是一个错误文档,无论如何都不能直接访问。 So this should simply be 404.php
(or 404.html
) and referenced as such in the ErrorDocument
directive.所以这应该只是404.php
(或404.html
)并在ErrorDocument
指令中被引用。 (You had also appended a trailing slash /404,/
, so this wouldn't have served the file anyway.) (您还附加了一个尾部斜杠/404,/
,因此无论如何这都不会提供该文件。)
For example:例如:
Options -MultiViews
ErrorDocument 404 /404.php
RewriteEngine On
RewriteRule ^login$ login.php [L]
RewriteRule ^([\w-]+)$ profile.php?username=$1 [L]
The shorthand character class \w
is the same as [a-zA-Z0-9_]
.简写字符 class \w
与[a-zA-Z0-9_]
相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.