htaccess:如果查询字符串中存在点(.),则会生成错误

发布于 2025-01-06 04:59:14 字数 1111 浏览 3 评论 0原文

我编写了 .htaccess 文件,如下所示:

Options +FollowSymLinks

RewriteEngine On

#open the product details page with the Product Number with PN prefix 
RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1 


#open the product search page for a particular category 
RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [NC]

#open the product search page for a particular category 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search =$1  [NC]

RewriteRule !\.(html|php)$ - [S=4]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]
RewriteCond %{ENV:uscor} ^Yes$
  1. 如果查询字符串值中存在点(.),则无法接受。现在,问题已经解决了,但是不能接受它之前的规则。

  2. 此外,如果给出了 URL“URL/products/PN123”或“URL/product/PN123”,我希望它正确重写“URL/product.php?pno=PN123”。请注意,如果使用正确的 CSS 正确给出“URL/PN123”,它可以正确重定向。并且“URL/product/PN123”也可以检索数据,但无法正常显示CSS。

I’ve written .htaccess file as follows:

Options +FollowSymLinks

RewriteEngine On

#open the product details page with the Product Number with PN prefix 
RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1 


#open the product search page for a particular category 
RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [NC]

#open the product search page for a particular category 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search =$1  [NC]

RewriteRule !\.(html|php)$ - [S=4]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]
RewriteCond %{ENV:uscor} ^Yes$
  1. It couldn't accept if there is dot(.) in the query string value. Now, it is solved, but can't accept its previous rule.

  2. Furthermore, I want it to rewrite the "URL/product.php?pno=PN123" properly if the URL "URL/products/PN123" or "URL/product/PN123" is given. Please note that it can redirect correctly if "URL/PN123" is given correctly with proper CSS. AND "URL/product/PN123" can also retrieve the data but can't show CSS prperly.

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

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

发布评论

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

评论(2

弥枳 2025-01-13 04:59:14

这是您更正/更新的 .htaccess,但是如果写更多关于您的实际需求的信息会更好:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

#open the product details page with the Product Number with PN prefix 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1 [L,NC,QSA]

#open the product search page for a particular category 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1  [L,NC,QSA]

#open the product search page for a particular category 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1 [L,NC,QSA]

RewriteRule !\.(html|php)$ - [S=4,NC]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]
RewriteCond %{ENV:uscor} ^Yes$

关于您的 css、js、图像文件:确保您的 css、js 等路径以斜杠 / 开头 而不是一个相对的。

This is your corrected/updated .htaccess, however it would have been better if had written more about your actual requirements:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

#open the product details page with the Product Number with PN prefix 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1 [L,NC,QSA]

#open the product search page for a particular category 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1  [L,NC,QSA]

#open the product search page for a particular category 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1 [L,NC,QSA]

RewriteRule !\.(html|php)$ - [S=4,NC]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes]
RewriteCond %{ENV:uscor} ^Yes$

About your css, js, image file: Make sure your path to css, js etc start with a slash / and not a relative one.

找回味觉 2025-01-13 04:59:14

您的正则表达式中有一些错误。如果您想使用,请在您的网站上加载此检查器这些。

  • 模式 ^((products/|product/|)PN[0-9-]+)/ 与“URL/products/PN123”或“URL/product/PN123”不匹配或“URL/PN123”并由于嵌入的匹配字符串和尾部斜杠而返回 PN123。您可以隐藏产品/并将尾随 / 设为可选,如下所示。

  • 我不喜欢使用!规则模式中的前缀,尤其是在使用分组部件时。太容易搬起石头砸自己的脚了。我更喜欢 cond 后跟空规则或否定前瞻。

  • 失败的匹配规则可能会触发后缀错误,所以我再次避免它们。引擎循环重试 .htaccess 文件(因为它们是在“每个目录上下文”中评估的)
    直到没有匹配,所以我发现总是使用 [L] 标志更安全。
  • 再次因为这个循环,您需要确保您的规则将终止,并且最好以简单透明的方式执行此操作。在这种情况下,你似乎不想
    重写映射到真实文件的 URI,所以为什么不将 -f 测试提升到顶部,然后所有其他规则都可以假设此条件为真。
  • 这两个搜索规则意味着如果请求是/acc123/等,则使用cat=acc123参数,否则使用search=。这是你想要的吗?
  • 您需要包含 QSA 标志,除非您想忽略任何现有参数。
  • 环境变量 USCOR(我坚持使用大写)在下一次传递时变为 REDIRECT_USCOR,因此这就是您测试的内容。我假设规则遵循最后一个条件

将这些放在一起得到:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# The rewrite engine loops in a Per Dir context. We don't remap real files so:

RewriteCond %{REQUEST_FILENAME}       -f
RewriteRule ^                         -                           [L]

# Open the product details page with the Product Number with PN prefix 
RewriteRule ^(?:products?)?(PN\d+)/?$ product.php?pno=$1          [L,NC,QSA]

# Open the product search page for a particular category
RewriteRule ^((?:bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1  [L,NC,QSA]

#open the product search page for a particular category 
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1           [L,NC,QSA]

RewriteRule \.(?!(php|html)$)               -                 [S=4,NC]
RewriteRule ^(.*?)_(.*?)_(.*?)_(.*?)_(.*)$  $1-$2-$3-$4-$5    [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*?)_(.*?)_(.*)$        $1-$2-$3-$4       [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*?)_(.*)$              $1-$2-$3          [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*)$                    $1-$2             [E=USCOR:Yes, L]

我不确定您现在想用 %{ENV:REDIRECT_USCOR} 做什么,而无需额外的规则和逻辑,尽管它将作为 $_SERVER["REDIRECT_USCOR']

You've got a few bugs in your regexps. Load up this checker on your site if you want to play with these.

  • The pattern ^((products/|product/|)PN[0-9-]+)/ does not match "URL/products/PN123" or "URL/product/PN123" or "URL/PN123" and return the PN123 because of the embedded match string and the trailing slash. You can hide the product(s)/ and make the trailing / optional as below.

  • I don't like using the ! prefix in rule patterns, especially when using a grouped parts. Too easy to shoot yourself in the foot. I prefer a cond followed by a null rule or a negative lookahead.

  • Fall-though match rules can trigger the postfix bug so again I avoid them. The engine loops retrying .htaccess files (because they are evaluated in a "Per Directory context")
    until no match so I find it just a LOT safer to always use the [L] flag
  • Again because of this looping you need to assure that your rules will terminate and its better to do this in a simple transparent manner. In this case you don't seem to want to
    rewrite URIs which map to real files so why not hoist the -f test to the top and all other rules can then assume that this condition is true.
  • The two search rule mean that the cat=acc123 parameter is used if the request is for /acc123/ etc and search= otherwise. Is this what you want?
  • You need to include the QSA flag unless you want to ignore any existing parameters.
  • The environment variable USCOR (I stick to uppercase) becomes REDIRECT_USCOR on the next pass so this is what you test for. I assume that a rule follows this last cond

Putting these all together gets:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# The rewrite engine loops in a Per Dir context. We don't remap real files so:

RewriteCond %{REQUEST_FILENAME}       -f
RewriteRule ^                         -                           [L]

# Open the product details page with the Product Number with PN prefix 
RewriteRule ^(?:products?)?(PN\d+)/?$ product.php?pno=$1          [L,NC,QSA]

# Open the product search page for a particular category
RewriteRule ^((?:bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1  [L,NC,QSA]

#open the product search page for a particular category 
RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1           [L,NC,QSA]

RewriteRule \.(?!(php|html)$)               -                 [S=4,NC]
RewriteRule ^(.*?)_(.*?)_(.*?)_(.*?)_(.*)$  $1-$2-$3-$4-$5    [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*?)_(.*?)_(.*)$        $1-$2-$3-$4       [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*?)_(.*)$              $1-$2-$3          [E=USCOR:Yes, L]
RewriteRule ^(.*?)_(.*)$                    $1-$2             [E=USCOR:Yes, L]

I am not sure what you want to do with %{ENV:REDIRECT_USCOR} now without the additional rules and logic, though it will be available to the script as $_SERVER["REDIRECT_USCOR'].

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