【问题标题】:htaccess make dynamic url to static urlhtaccess 将动态 url 转换为静态 url
【发布时间】:2017-05-08 08:36:56
【问题描述】:

我有一个类似的网址:

http://localhost/admin-s/index.php?m=user

但是,我想要这样的网址:

http://localhost/admin-s/user

我尝试这样:

RewriteRule ^([a-z0-9]+).php$ index.php?nol&m=$1 [L]
RewriteCond %{QUERY_STRING} ^m=([a-zA-Z0-9]+)$
RewriteRule ^index.php$ user%1.php [L]

如何制作?

【问题讨论】:

  • 不清楚你想做什么:你想将http://localhost/admin-s/index.php?m=user重写为http://localhost/admin-s/user(如果是这样,你想只是内部重写还是重定向?)或另一种方式?这两个应该是用户在浏览器中看到的 URL?
  • 我使用了 ajax,在文件夹用户中,我有三个文件:user.php(这就像 index.php),user_crud.php(用于 php 中的 CRUD),user_ss.php(服务器端),我希望用户只看到 localhost/admin-s/user

标签: php .htaccess


【解决方案1】:

希望这个简单的配置对您有所帮助。可以查看这个配置here

这会将这个模式的请求从http://localhost/admin-s/index.php?m=user重定向到http://localhost/admin-s/user

说明:

1.如果REQUEST_URI/admin-s/index.php

2.如果QUERY_STRINGm=someWords

3.重定向到/admin-s/someWords

.htaccess

RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_URI} \/admin\-s\/index\.php
RewriteCond %{QUERY_STRING} m=([\w]+)
RewriteRule ^(.*)$ /admin-s/%1? [R=301,L]

【讨论】:

  • 嗨,它不起作用。但我仍然错误,我将重定向到localhost/admin-s/user/user.php的网址
  • if (isset($_GET['m']) == NULL) { include 'home.php'; } elseif ($_GET['m'] == 'user') { 包括'./user/user.php'; }
  • 你期望的输出是include 'home.php'; 对吧?
  • 在浏览器的隐身模式下试试这个.htaccess 它会正常工作,您可以检查htaccess.mwl.be,只需将我的.htaccess 代码放在这里并尝试使用任何网址。
  • 您的.htaccess 是真的。但我对isset 有意见。 if (isset($_GET['m']) == NULL) { include 'home.php'; } elseif ($_GET['m'] == 'user') { include './user/user.php'; } elseif ($_GET['m'] == 'model') { include './model/model.php'; }
【解决方案2】:

问题在这里得到了回答:How to turn dynamic URL into static URL

您的问题可以尝试以下规则:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^admin-s/([^-]+) /index.php?m=$1 [R=301,QSA,L]

让我们知道它是否有效

【讨论】:

    【解决方案3】:

    您可以在 root/.htaccess/admin-s/.htaccess 中使用以下规则:

    RewriteEngine on
    
    #redirect /admin-s/index.php?m=user to /admin-s/user
    RewriteCond %{THE_REQUEST} /admin-s/index\.php\?m=([^\s&]+) [NC]
    RewriteRule ^ /admin-s/%1? [L,R]
    #rewrite the new uri back to the orignal location
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(?:admin-s/)?(.+)$ /admin-s/index.php?m=$1 [NC,L]
    

    【讨论】:

    • 谢谢你的回答,但我在isset. if (isset($_GET['m']) == NULL) { include 'home.php'; } elseif ($_GET['m'] == 'user') { include './user/user.php'; } elseif ($_GET['m'] == 'model') { include './model/model.php'; }有问题
    • 在 index.php 中,包含 bootstrap、jquery 等,所以当用户看到 localhost/admin-s/user 时必须加载 index.php
    【解决方案4】:

    在您的admin-s 目录中尝试以下操作,

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([\w-]+)$ index.php?m=$1 [L]
    

    【讨论】:

      猜你喜欢
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多