我有一个网站,我希望替换一部分网址https://example.com/[THISPART]/file.ext
。
将在网站上请求的示例网址:
https://example.com/example-page/sw.js
https://example.com/example-page/image.jpg
https://example.com/some-other-page/sw.js
https://example.com/some-other-page/file.pdf
https://example.com/page-with-attitude-4/sw.js
https://example.com/page-with-attitude-4/info.txt
我想如何重写它们:
https://example.com/content/example-page/sw.js
https://example.com/example-page/image.jpg
https://example.com/content/some-other-page/sw.js
https://example.com/some-other-page/file.pdf
https://example.com/content/page-with-attitude-4/sw.js
https://example.com/page-with-attitude-4/info.txt
换句话说,如果只请求sw.js,则重写到其他URL。 到目前为止,我在htaccess中使用的是:
RewriteRule ^(.*)\/sw.js$ content/$1/sw.js [L]
我一直在使用http://htaccess.mwl.be/作为测试人员并且测试显示正常,但是当我在网站上使用它时,它不起作用。有什么帮助吗?
答案 0 :(得分:1)
做到了!
RewriteRule ^([A-Za-z-]+)/sw\.js$ /places/$1/sw.js [L,QSA]
答案 1 :(得分:1)
将以下代码放在主目录.htaccess
文件中:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} sw\.js
#the line above to match sw.js
RewriteCond %{THE_REQUEST} !content
# the line above to exclude any request including content from the following rule
RewriteRule ^(.*)$ content/$1 [R=302,L,NE]
#the line above to apply redirection for requests that passed previous conditions and redirect any request ended with sw.js
RewriteRule ^content/(.*)$ /$1 [L,QSA]
#the last line is to make internal redirection for original path
经过测试,如果确定,如果您想进行永久重定向,请将302
更改为301