【问题标题】:how to change the URL from a GET-variable to imaginary directory如何将 URL 从 GET 变量更改为虚构目录
【发布时间】:2014-04-02 09:52:29
【问题描述】:

网站中的所有链接都是一个获取变量。用户总是打开 index.php 并给出不同的 get-variables,它定义了不同的内容。

示例: 主页是 example.com/?p=1 联系页面是 example.com/?p=7

现在我希望 URL 看起来像 example.com/contact。而且由于页数不是静态的,我无法为每个页面创建一个目录。

可能我需要一种方法将我的 index.php (example.com?p=3) 的内容导入一个不存在的路径 (example.com/new-path)。

我听说有一种方法可以使用 .htaccess 文件来解决这个问题。

【问题讨论】:

标签: php .htaccess redirect directory


【解决方案1】:

我不是 .htaccess / mod_rewrite 专家,但只是发现这个可能对你有用。请注意,您必须为要重定向的每个页面提供一个条目,因为系统本身不会知道数字和页面之间的关系。您可能需要稍微摆弄一下才能尝试删除index.php

# Original URL: 
# http://www.example.com/index.php?p=1

# Desired destination URL:
# http://www.example.com/path-to-new-location/

# .htaccess syntax:

RewriteEngine on
RewriteCond %{QUERY_STRING} p=1
RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]
# Redirect URLs with query parameters (files placed in subdirectory)

注意,# 是 cmets 的标志,该行上的任何内容(灰色)都将被忽略,其唯一目的是为您提供有关代码的信息 / cmets。

来源(第 52-63 行):Common .htaccess Redirects - Gist

【讨论】:

  • 这是重定向到新位置的好方法。但我需要一个名为“path-to-new-location”的文件夹......基本上我需要一种在不改变内容的情况下更改 URL 的方法
猜你喜欢
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 2018-07-13
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 2021-09-30
相关资源
最近更新 更多