RewriteRule 更改文件的物理路径
假设我的网站的域名是 [http://mywebsite.com][1],它会打开 index.php。该脚本从 url 获取 $page、$section、$language 变量。因此 [http://mywebsite.com/index.php?lang=en§ion=home&page=sitemap][2] 打开属于“home”部分的英文站点地图页面。我希望将相同的网址重写为 [http://mywebsite.com/home/sitemap_en.html][3]。为了实现这一点,我已经将以下内容放入 .htaccess 中: RewriteCond %{REQUEST_URI} .+\/.+ RewriteRule ^(.+)\/(.+)_(mk|en|al)\.html$index.php?lang=$3§ion=$1&page=$2 [L]
但现在有一个很大的问题。当我访问这样的网址时,找不到文件,因为文件 style.css 位于根文件夹中而不是 [http://mywebsite.com/home/style.css][4] 中,并且服务器在那里正在寻找它。 “home”不是真正的文件夹,它不存在,它只是一个部分。对于所有 jpg、png、js、gif 等也是如此。如何以我喜欢的方式重定向页面,并使用真实路径找到文件?
ps 像 [http://mywebsite.com/index.php?lang=en§ion=contact][5] 这样的部分根本没有页面。应像这样联系他们:[http://mywebsite.com/contact_en.html][6] 在上一条规则之后,我为他们准备了这个: RewriteRule ^(.+)_(mk|en|al).html$ index.php?lang=$2§ion=$1
Let's say the domain for my website is [http://mywebsite.com][1] and that opens the index.php. That script gets the $page,$section,$language variables from the url. So [http://mywebsite.com/index.php?lang=en§ion=home&page=sitemap][2] opens the sitemap page in English which belongs to the "home" section. And I want that same url to be rewritten to [http://mywebsite.com/home/sitemap_en.html][3]. To achieve this, already I've put the following in the .htaccess:RewriteCond %{REQUEST_URI} .+\/.+
RewriteRule ^(.+)\/(.+)_(mk|en|al)\.html$ index.php?lang=$3§ion=$1&page=$2 [L]
But there is a huge problem now. When I visit some url like that, the files are not found because the file style.css is in the root folder and not in [http://mywebsite.com/home/style.css][4] , and there the server is searching for it. "home" is not real folder and it doesn't exists, it's only a section. The same goes for all the jpg, png, js, gif etc. How can I redirect the pages the way I like, and the files to be found with the real paths?
p.s. Some section like [http://mywebsite.com/index.php?lang=en§ion=contact][5] don't have pages at all. They should be reached like so: [http://mywebsite.com/contact_en.html][6]
I have this for them, after the previous rule: RewriteRule ^(.+)_(mk|en|al).html$ index.php?lang=$2§ion=$1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在标头中使用 base 标记,以便所有相对路径都远离特定路径href so:
会导致所有相对(不仅仅是 css)URL 从您的根目录加载
,或者
如其他人所述,只需在前面放置一个前导“/”即可使您的路径成为绝对路径:
You can use a base tag in your header so that all relative paths are off of a specific href so:
would cause all relative (not just css) URL's to be loaded off your root directory
OR
as stated by others just make your paths absolute by putting a leading "/" at the front:
您是否尝试通过前缀斜杠来相对于顶级目录(主目录)引用 css 和 js 文件等?即,如果将它们保存在站点主目录中名为 styles 的目录中:
Did you try and make the references to css and js files etc relative to the top level directory (the home directory) by prefixing with a slash? ie if you keep them in a directory called styles in the sites home directory: