【发布时间】:2021-12-23 19:15:57
【问题描述】:
我的示例网站名称是example.com/customer/?loc=dashboard,这个网站是使用PHP 编码的。我需要将姓氏?loc=dashboard 更改为dashboard。这意味着我希望结果网站名称是mydomain.com/customer/dashboard
我在我的根文件夹中添加了一个名为 .htaccess 的文件,并添加了类似的内容,但它无法正常工作:
ReWriteEngine On
RewriteRule ^/dashboard /?loc=dashboard [L]
我已经参考这个网站https://mediatemple.net/community/products/dv/204643270/.htaccess-rewrite-rules 去做。但不能工作。希望有人能指导我如何解决这个问题。谢谢。
更新 - 1
使用@RavinderSingh13 方法结果:
?loc=dashboard 之前的结果:
更新于 2021 年 12 月 11 日 09:19:00
重写规则
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com\.my [OR,NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com.my/$1 [R,L]
RewriteCond %{HTTP_HOST} ^example\.com\.my [OR,NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com.my/$1 [R,L]
##External redirect to /customer/dashboard url rules here.
RewriteCond %{THE_REQUEST} \s/customer/?\?loc=(\S+)\s [NC]
RewriteRule ^ /customer/%1? [R=301,L]
##Internal rewrite rules to get it served by index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customer/(.*)/?$ index.php?parameter=$1 [QSA,NC,L]
更新 - 2
everyoneknows.come.my/.htaccess
everyoneknows.come.my/index.php
everyoneknows.come.my/customer
everyoneknows.come.my/customer/index.php
everyoneknows.come.my/customer/account/dashboard.php
因此,在我的 php 代码中,我使用 ?loc= 替换 account,然后变成everyoneknows.come.my/customer/?loc=dashboard
更新-3
我正在使用这种方法获取?loc:
if (isset($_GET['loc'])) {
if (file_exists('account/' . $_GET['loc'] . '.php')) {
// if ($module_user_permission['view'] == 1) {
include_once 'account/' . $_GET['loc'] . '.php';
// } else {
// if ($_GET['loc'] != 'home') {
// include_once 'app/access_denied.php';
// } else {
// include_once 'account/dashboard.php';
// }
// }
} else {
include_once 'account/dashboard.php';
}
} else {
include_once 'account/dashboard' . (isset($_GET['system']) ? "_" . $_GET['system'] : "") . '.php';
}
【问题讨论】:
-
1.在 .htaccess 中配置时,路径 RewriteRule 匹配,从不以前导斜杠开头,此时已被剥离。
-
2.你说你把它放在根目录下,所以你要匹配的路径应该是
customer/dashboard,而不仅仅是dashboard。替换 URL 也是如此。 -
@CBroe 所以一定要这样写
RewriteRule ^customer/dashboard /?loc=dashboard [L],对吧? -
你想重写到
mydomain.com/?loc=dashboard...吗? -
是的。是index.php
标签: .htaccess url mod-rewrite url-rewriting