【问题标题】:show index.php on every html or php url, without changing url [closed]在每个 html 或 php url 上显示 index.php,而不更改 url [关闭]
【发布时间】:2013-10-23 07:00:50
【问题描述】:

在每个 html 或 php url 上显示 index.php,而不更改 url

例如。用户输入任何东西-everything.html(.php) 将显示 index.php 而不更改 url,

条件 - 主机上不存在任何东西-everything.html(.php)

喜欢 404 重定向而不更改 url

【问题讨论】:

    标签: php .htaccess url redirect


    【解决方案1】:

    欢迎来到 SO。如果您在 Apache 网络服务器上运行您的网站,您可以使用 Apache Module mod_rewrite 来实现此目的。这是一个 .htaccess 文件的示例,您必须将其放在网站的根目录中。

    # switch on mod_rewrite
    RewriteEngine On
    
    # redirect all non-www request to www
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # rewrite all physical files and folders
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # add all exceptions that should not be redirected
    RewriteCond %{REQUEST_URI} "/layout/" [OR]
    RewriteCond %{REQUEST_URI} "/javascript/" [OR]
    RewriteCond %{REQUEST_URI} "/content"
    
    # and finally pass everything to the index.php as a parameter
    RewriteRule .* - [L]
    RewriteRule (.*) index.php?value=$1 [QSA]
    

    这将执行以下操作:

    • 如果用户请求http://domain.com,他将被转发到http://www.domain.com
    • 请求 domain.com/hello/world.html 将在 index.php 文件中结束,其中包含现有的 $_GET['value'] 包含 hello/world.html,然后可以进一步处理李>

    【讨论】:

    • 工作,但每个页面显示相同的回显需要根据 url 不同的 php 回显
    • @kritprim 抱歉,我不太理解您的评论。您的意思是,您需要为每个请求使用不同的 PHP 文件,例如 /hello 将被重定向到 hello.php
    • 当前页面的 echo urlof php 文件
    • 所有页面需要不同的回显
    • 前。这是 php
    【解决方案2】:

    使用 apache 重写(如果您使用 apache 并且可以访问服务器)。 Apache rewrite all paths to index.php 的配方应该没问题。

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 2017-04-12
      • 2013-06-17
      • 2013-07-21
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多