[英].htaccess file rewrite rule check
I want to rewrite my apache mod_rewrite pattern. 我想重写我的Apache mod_rewrite模式。 I use to do with following:
我曾经做以下事情:
RewriteEngine on
RewriteRule /^([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
With the above rule I can't get what I expecting. 按照上述规则,我无法实现我的期望。 Can anyone tell what is wrong with my code and how to solve it.
谁能告诉我代码有什么问题以及如何解决。
Edited. 编辑。 my requirement is simple.
我的要求很简单。 I want to redirect if i enter /1.html to /search.php?search_id=1
如果我将/1.html输入到/search.php?search_id=1,我想重定向
Thats it. 而已。
This should work: 这应该工作:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !search\.php
RewriteCond %{REQUEST_URI} ^/([0-9]+)\.html [NC]
RewriteRule .* search.php?search_id=%1 [L,QSA]
Maps silently 静默地图
http://example.com/FileID.html
To: 至:
http://example.com/search.php?search_id=FileID
search.php
is considered a fixed string, while FileID
can be any number. search.php
被认为是固定字符串,而FileID
可以是任何数字。
For permanent redirection, replace [L,QSA]
with [R=301,L,QSA]
要进行永久重定向,请将
[L,QSA]
替换为[R=301,L,QSA]
You have a typo in your rule try this: 您的规则中有错字,请尝试以下操作:
RewriteEngine on
RewriteRule ^/([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
By the way you could also try to use this: 顺便说一句,您也可以尝试使用此方法:
RewriteRule ^/?([^A-Z0-9a-z]+)\.html /search.php?search_id=$1&%{QUERY_STRING}
This makes the leading slash optional. 这使斜杠成为可选。
Are you typing 1.html after the domain? 您在域后输入1.html吗? That is if your domain is example.com, then you type example.com/1.html?
就是说,如果您的域名是example.com,那么您键入example.com/1.html? If it is you are doing then don't use / before the regular expression in .htaccess file.
如果您正在这样做,则不要在.htaccess文件中的正则表达式前使用/。
Try adding the following into your main .htaccess file. 尝试将以下内容添加到主.htaccess文件中。 That is located in the directory which has your home page of the website:
该目录位于具有您的网站主页的目录中:
RewriteEngine On
#RewriteBase /
RewriteRule ^([A-Za-z0-9]+)\.html/?$ /search.php?search_id=$1 [NC, L]
I have used RewriteBase as a comment. 我已经使用RewriteBase作为注释。 If you use a shared server then keep it as a comment or delete the line.
如果您使用共享服务器,则将其保留为注释或删除该行。
EDIT: [NC, L] use to tell that, NC flag for rule no need to be case sensitive (no case) and L flag for stop if this was matched. 编辑:[NC,L]用来告诉您,规则的NC标志不需要区分大小写(无大小写),如果匹配,则L标志用于停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.