Apache RewriteRule无法正常工作

时间:2018-07-16 06:58:34

标签: regex apache mod-rewrite url-rewriting

我在URI上有一个PHP页面(index.php),如下所示:

UITextField.appearance().tintColor = .green
UITextView.appearance().tintColor  = .green

php页面需要参数http://example.com/my-folder/

?location=name

我在.htacces上创建这样的重写规则

http://example.com/my-folder/?location=new-york
or
http://example.com/my-folder?location=dallas

预期结果是

使用如下网址:

<ifModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  RewriteCond %{HTTPS} !=on
  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*?)\/my-folder\/?(.*?)$ my-folder/$2?location=$1 [L]

</IfModule>

重定向到:

http://example.com/new-york/my-folder/

似乎可行,但是我遇到了一个问题,没有斜杠

不带反斜杠的网址会加载索引,但无法加载CSS和JS。

代码:

http://example.com/my-folder?location=new-york

尝试加载资源:

<script src="js/async.js?v=f373bdf3dc0" async></script>

代替预期的URL

https://example.com/new-york/js/async.js?v=f373bdf3dc0

1 个答案:

答案 0 :(得分:0)

将您的.htaccess设置为:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/((?:.*/)?my-folder(?:/.*)?)$ $2?location=$1 [L,QSA,NC]

然后将其添加到页面HTML的<head>部分的下方:<base href="/" />,以便从该基本URL(而不是从当前页面的URL)解析每个相对URL

相关问题