htaccess - 重写 url
如何将 URL 重写
http://example.com/file_name
为
http://example.com/depictions/file_name.html
:例如:http://example.com/application
到http://example.com/depictions/application.html
How can I rewrite the URL:
http://example.com/file_name
to:
http://example.com/depictions/file_name.html
For example: http://example.com/application
to http://example.com/depictions/application.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我们假设
file_name
不能包含点(这会立即排除对通常包含文件扩展名的真实文件的任何请求 - 即由点分隔)。然后,您可以使用 mod_rewrite 执行类似以下操作:这实际上将
file_name
限制为字符0-9
、az
、AZ
、_
(下划线)和-
(连字符),由正则表达式字符类[\w-]
表示。因此,这只检查对文档根目录中“文件”的请求(即不允许使用斜杠)。这还会在发出内部重写之前测试相应的目标
.html
文件是否存在。$0
反向引用包含与RewriteRule
模式 匹配的整个 URL 路径。It makes it a bit easier in terms of optimisation if we assume the
file_name
cannot contain dots (this immediately excludes any requests for real files that ordinarily include a file extension - which is delimited by dot). Then you can do something like the following using mod_rewrite:This actually limits the
file_name
to the characters0-9
,a-z
,A-Z
,_
(underscore) and-
(hyphen) as denoted by the regex character class[\w-]
. Consequently, this only checks requests to "files" in the document root (ie. no slashes permitted).This also tests that the corresponding target
.html
file exists before issuing the internal rewrite.The
$0
backreference contains the entire URL-path that is matched by theRewriteRule
pattern.对于每个
file_name
:For every
file_name
: