当应用重写规则时,友好URL部分工作



我在应用重写规则时遇到.htaccess问题。下面我解释我所做的:

在localhost:中

路径:/dev/Site

所需的友好URL:www.site.com/l/p->l用于语言,p适用于页面

源url是:www.site.com/index.php?p=大约&l=en

in.htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?p=$2&l=$1 [L]

情况是重写规则部分起作用

1-www.site.com/en/about可以工作,但当位于/dev/site/template/ing的img文件夹无法获取文件时,因为需要以下结构:http://127.0.0.1/dev/Site/en/templates/img/


HTML(例如:)

<h1 class='center'>
  <a href='http://www.Site1.com/index.php?p=about&l=dev/Site1/es' >
    <img src='**templates/img/logo.png**' width='204' height='40' alt='Site1 Logo' /></a>
</h1>

我发现重写规则在href的url中应用了更改。。

这可能是由于我在您的代码中发现的几个原因,您的代码当前如下所示:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?p=$2&l=$1 [L]

但我认为你的代码应该是:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?p=$2&l=$1 [L,QSA]

我所做的改变是:

  • +更改为*+表示一个或多个,而*表示零或多个。如果您正在寻找img路径,则您的htaccess中没有这两个变量)
  • 在规则中添加了QSA(这会使查询字符串附加,所以基本上你是在向GET添加字符串,我相信?)
  • [^/]更改为.(点表示任何字符都应该执行您想要的操作)
  • 将重写器的基设置为根(/dev/Site

我希望我已经帮助了你,我应该建议你做一些关于htaccess的研究。


编辑1:

尝试更改图像,包括这样的:

<h1 class='center'>
  <a href='http://www.Site1.com/index.php?p=about&l=dev/Site1/es' >
    <img src='/templates/img/logo.png' width='204' height='40' alt='Site1 Logo' /></a>
</h1>

变化:

  • 将图像的加载从template/img/logo.png更改为/template/img/logo.png(您的包含方式(路径前没有/)告诉服务器必须从url加载,使用从根开始的反斜线)

最新更新