我做了一个重写规则,匹配了6个字符的文档名称,它成功了。
RewriteRule ^document\/(.{6})\/?$ document/?name=$1 [NE,L]
由于我知道文档最多只能命名12个字符,因此我添加了最大长度量词。但是,使用它会产生500服务器错误:
RewriteRule ^document\/(.{6,12})\/?$ document/?name=$1 [NE,L]
事实上,我得到了以下结果:
(.{6})
有效
(.{6,})
个错误
(.{6,7})
有效
(.{6,8})
有效
(.{6,9})
个错误
等等。
我还应该提到https://www.regexpal.com/?fam=109235告诉我,我的规则没有任何问题。但是我仍在使用500 Server错误。
谢谢@emma,示例URL会被重写:
答案 0 :(得分:1)
我认为您使用此设置进入了重定向循环。
添加RewritCond
来检查请求中是否没有查询字符串
RewriteCond%{QUERY_STRING} ^ $
RewriteRule ^ document /([[^ /] {6,12})/?$ document?name = $ 1 [NE,L]
答案 1 :(得分:0)
我不太确定您如何编写此RewriteRule。但是,this tool可能会帮助您首先找到一个表达式,然后编写并测试RewriteRule。我很确定,您可以不使用量词就可以编写它。例如:
document\/([0-9]+)
将传递您的示例URL。
然后,如果您只希望将name的前六位替换为数字,则可能需要编写类似于以下内容的RewriteRule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} mywebsite\.com [NC]
RewriteRule document\/([0-9]+) document\/?name=$1 [NE,L]
</IfModule>
对于是或否尾部斜杠,这些可能有效:
# No Trailing Slash Policy, if you wish no slash at the end of your URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
# Trailing Slash Policy, if you wish to have a slash at the end of your URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1/ [L,R] # <- for test, for prod use [L,R=301]