首页 > 解决方案 > .htaccess 重写条件/规则并隐藏 url 参数

问题描述

我的根目录中有一个 htaccess。

我的文件路径http://example.com/folder1/folder2/index.php?country=eu&lang=en

我在 htaccess 中做了这个。

RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$   
RewriteRule ^([^/]*)/([^/]*)$ /folder1/folder2/index.php?country=$1&lang=$2 [L]
RewriteRule ^([^/]*)$ /folder1/folder2/index.php [L]

看起来像这样

http://example.com/index.php?country=eu&lang=en

但我想要这样-> http://example.com/eu-en

有没有办法在 htaccess 中做到这一点?

编辑: 我必须使用重写条件,因为我重定向到文件夹取决于主机。

编辑 2:解决方案

RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$   
RewriteRule ^([a-z]{2,2})-([a-zA-Z0-9_-]+)$ /folder1/folder2/index.php?country=$1&lang=$2 [QSA]

感谢答案,它现在正在工作。但在那之后我在网站上得到了 css 和 js 错误。然后我更改了css和js路径/folder1/folder2/css。

标签: apache.htaccessmod-rewriteurl-rewritingurl-parameters

解决方案


试试下面,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)$ index.php?country=$1&lang=$2 [QSA]

推荐阅读