【问题标题】:.htaccess redirect to subdomain and hide in uri?.htaccess 重定向到子域并隐藏在 uri 中?
【发布时间】:2019-08-10 08:43:47
【问题描述】:

我想重定向每个请求:

http://example.com/page1.php (<-non-existent url to file)

http://example.com/content/sites/page1.php (<-real path to file in folder structure)

在继续展示的同时:

http://example.com/page1.php

在浏览器中并使用位于根文件夹中的 .htaccess 文件。

这有可能吗?在此先感谢:)

【问题讨论】:

  • 粘贴你在 htaccess 中所做的事情
  • @MohammedElhag 到目前为止,我只有文件扩展名代码:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php
  • 是的,这正是 URL 重写的工作原理。您应该考虑使用路由器(将所有内容发送到 index.php 并从那里分派)
  • @LarsStegelitz 我最初的想法是将每个不存在的 url(由 history.pushstate() 生成)重定向到 index.php,内容通过 ajax 加载
  • 是的,没错,大型框架就是这样做的:物理上不存在的所有内容(可从网络访问)都通过 index.php 进行路由

标签: php .htaccess


【解决方案1】:

根据您的评论,以下规则就是您所做的:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)$ $1.php

他们会检查请求是否不是目录,然后检查它是否接受.php 然后重写它,根本不需要。

一般来说只试试这个:

RewriteEngine On 
RewriteRule ^([^\/]+\.php)$   content/sites/$1 [L]

如果您只希望 page1.php 使用它:

RewriteEngine On 
RewriteRule ^(page1\.php)$  content/sites/$1 [L]

注意:清除浏览器缓存然后测试`

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2011-04-29
    相关资源
    最近更新 更多