简体   繁体   English

如何阻止 htaccess 重写规则重写所有 url

[英]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

I just wanted localhost/qwerty to display profile.php anything else should show the normal page and if it does not exists it shows a 404 error 我只是想让 localhost/qwerty 显示 profile.php 其他任何内容都应该显示正常页面,如果它不存在则显示 404 错误


`profile.php` contents `profile.php` 内容
                                               htdocs(xampp)
                                                   |
 =======================================================================================
 |         |          |              |                       |                         |
login,    404,     style.css      profile.php              index.php               .htacccess



NOW MY FILE TREE 现在我的文件树
 htdocs(xampp) | ======================================================================================= | | | | | | login, 404, style.css profile.php index.php.htacccess




Now the problem is that none of the style sheets (or the javascript files) load, all the pages on http://localhost are now assumed to be `profile.php`, and in `profile.php` $_GET['username'] displays `PROFILE.PHP' instead of 'USER123' 现在的问题是,没有任何样式表(或 javascript 文件)加载,http://localhost 上的所有页面现在都假定为 `profile.php`,并且在 `profile.php` $_GET['username '] 显示 `PROFILE.PHP' 而不是 'USER123'
Just to be clear i want it that if http://localhost/login, is the url then login, loads(i even added a comma at the end of login to make the .htaccess not to look at login as a username, but that doesn't work) 只是要清楚我想要它,如果 http://localhost/login 是 url 然后登录,加载(我什至在登录结束时添加了一个逗号以使 Z8052C42AB3B8AA06CCA3F5F788A4 登录,但不是将用户名视为登录名不起作用)
 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).但是,这仍然不适用于login404 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM