仅针对.php文件的重写规则

时间:2016-01-15 10:25:47

标签: php html .htaccess

我需要重写这个网址:

mysyte.com/index.php?lang=it

为:

mysite.com/it/index

无需重写css href,javascript src和image src。 我尝试了很多解决方案,但都没有用,有人能帮助我吗?

谢谢!

编辑:

我试过了:

RewriteEngine On
RewriteRule ^(.*)/(.*) $2.php?lang=$1

但这也会覆盖我的css电话。

然后我尝试了:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*) $2.php?lang=$1

具有相同的结果

2 个答案:

答案 0 :(得分:1)

编辑: 在.htaccess中,写下以下几行:

# Activate url rewriting
RewriteEngine on
# Rewrite all url of the form /2-letters-language-code/index to index.php/lang=2-letters-language-code
RewriteRule ^([a-z]{2})/index$ /index.php?lang=$1 [QSA,L]

正则表达式的([a-z]{2})部分意味着“每组两个字母”,因此它将捕获“它”,但也会“fr”,“en”......以及两个字母中的所有其他语言代码。如果这种情况过于笼统,可以根据需要将其替换为(it | en | fr)。

如果您不仅要将index重写为index.php,而且还需要重写任何字母数字字符串,您可以这样做:

RewriteRule ^([a-z]{2})/([a-zA-Z0-9_]+)$ /$2.php?lang=$1 [QSA,L]

注意在第二个括号中不要太大,否则规则将捕获你不想要的字符串。例如,[a-zA-Z0-9_]+表示:每组由1个或多个字母数字字符和下划线组成。它排除斜杠(/)和连字符( - )。

答案 1 :(得分:1)

这已在这里得到解答: CSS not loading after redirect with htaccess rewrite rule

  

解决此问题的最简单方法是在HTML文件的def create @client = Client.new(params[:client]) if @client.save redirect_to @client, notice: t_notice('controllers.successfully_created', Client) else render action: "new" end end def update @client = Client.find(params[:id]) if @client.update_attributes(params[:client]) redirect_to @client, notice: t_notice('controllers.successfully_updated', Client) else render action: "edit" end end 中指定CSS文件的完整路径。

     

如果您的mod_rewrite规则正在修改CSS文件所在的路径,那么您也希望将其放在<head>之前:

RewriteRule
     

这可以确保请求在重写之前不匹配文件。