Why can't i have slashes in my rewrite rule?

拥有回忆 提交于 2020-01-25 03:36:05

问题


I am trying to make an .htaccess file, to create SEO url.

It seems however that I can't have a slash in the mod rewrite, am I doing this wrong?

My code is as follows:

RewriteEngine on
RewriteRule ^(.+)/$ /$1 [L,R=301]
RewriteRule ^folder/folder-name index.php?show=folder&folder_id=7

The page is loaded correctly, but the style sheet/images/javascript aren't found!

It's as if, the page thinks that it should look for the style/images/js relative to "folder/folder-name".

If i change the last line to:

RewriteRule ^folder-folder-name index.php?show=folder&folder_id=7

Then everything is loaded correctly, so i'm pretty sure it's because of the slash.

Any help?


回答1:


It is because you're using relative paths in your JS, CSS etc. You have 2 options to fix it:

  1. Just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
  2. You can try adding this tag in your page's header:

    <base href="/" />
    



回答2:


One alternative: You can use paths relative to site root. i.e. change:

<img src="images/some.jpg" />

by

<img src="/images/some.jpg" /> 

Being images folder here: yoursite.com/images



来源:https://stackoverflow.com/questions/20616831/why-cant-i-have-slashes-in-my-rewrite-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!