【问题标题】:How to use RewriteRule like WordPress and Drupal如何像 WordPress 和 Drupal 一样使用 RewriteRule
【发布时间】:2017-01-02 19:08:23
【问题描述】:

我有一个包含多个变量的包含脚本的网站。

以下是一些类型的 URL 和变量:

Main page: index.php
Login page: login.php (redirects to index.php after login).
Logout page: logout.php (redirects to login after logout).
Profile page: index.php?id=profile
Users password page: index.php?id=profile&sub=password

有些页面的 URL 中甚至还有更多的变量。我想使用 RewriteRules 来获得这样的好 URL:

url.com/my/site/ (base)
url.com/my/site/profile/ (profile page)
url.com/my/site/profile/password/ (password page)
url.com/my/site/profile/password/variable1/variable2/variable3/ (use variables in the URL)

如何在 .htaccess 文件中执行此操作?该站点不是域上的根,而是几个文件夹。

【问题讨论】:

    标签: php mod-rewrite


    【解决方案1】:

    .htaccess 规则适用于其容器目录及其所有子目录。这可能是一个很好的起点:

    <IfModule mod_rewrite.c>
        # Removing .php file extension.
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME}\.php -f
        RewriteRule ^(.*)$ $1.php
    </IfModule>
    

    或者您可能想从 url 中删除 index.php 的 GET 参数:

    <IfModule mod_rewrite.c>
        # Removing GET parameters.  
        RewriteRule ^index/([^/\.]+)/?$ index.php?id=$1 [L]
    </IfModule>
    

    阅读更多:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多