【问题标题】:Rewrite url name using with .htaccess使用 .htaccess 重写 url 名称
【发布时间】: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


【解决方案1】:

根据您的尝试,样品;请尝试遵循 htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

确保您的 htaccess 规则文件与 customer 文件夹一起存在(不在其中)。在第二个重写规则中,我添加了parameter 以使其传递到index.php 文件,您可以根据您的要求进行设置。

RewriteEngine ON
##Applying www by external redirect rules here...
RewriteCond %{HTTP_HOST} ^example\.com\.my [NC] 
RewriteCond HTTPS off
RewriteRule ^(.*)/?$ https://www.example.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 dashboard.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customer/(.*)/?$ customer/account/dashboard.php?parameter=$1 [QSA,NC,L]

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 2020-09-09
  • 1970-01-01
  • 2021-06-29
相关资源
最近更新 更多