【问题标题】:Removing 2 folders from URL and redirecting to page从 URL 中删除 2 个文件夹并重定向到页面
【发布时间】:2021-07-17 11:30:24
【问题描述】:

我正在创建一个新的网络应用程序,这就是我的项目文件夹的样子:

如您所见,我已将根文件夹中的 htaccess 重命名,因为我没有使用该 htaccess,而是使用位于公用文件夹内的那个。我正在尝试更改我的网址:

http://example.site/public/pages/about

到:

http://example.site/about

我能够从文件扩展名中删除 .php,但我添加的用于从 URL 中删除文件夹的代码不起作用。

注意:在根文件夹 (App/index.php) 中的 index.php 文件只有这样:

<?php

header('Location: public/pages');

所以我只是用它来发送到正确的文件夹。

这是我目前所拥有的:

Options +FollowSymLinks -MultiViews

RewriteEngine on

RewriteBase /App/public/

RewriteRule ^pages/(.+)$ /$1 [L,R=302,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) $1.php [NC,L]

我从这个网址看到了这个问题/答案:

Remove / Rewrite 2 directory names from URL

但这对我不起作用。期待能帮忙的人,谢谢。

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    假设 ``htdocs/app/is yourDocumentRootforapp.blu` 域。

    htdocs/app/public/.htaccess 内你可以使用这个代码:

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    
    # if URL has /pages/ then remove and redirect
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^pages/(.+)$ /$1 [L,R=301,NC]
    
    # landing page for app subdomain to show index.php
    RewriteRule ^$ pages/index.php [L]
    
    # check & rewrite if a matching .php file exists in pages/ subdirectory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/public/pages/$1.php -f
    RewriteRule ^(.+?)/?$ pages/$1.php [L]
    

    除此之外,您还需要在htdocs/app/.htaccess 中使用此代码:

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    
    # send all the traffic to /public/
    RewriteRule .* public/$0 [L]
    

    【讨论】:

    • 它对我不起作用,它仍然显示相同的 url,即使我删除它们,它仍然会重定向到其中包含 public/pages/ 的相同 url。当我在 htdocs 文件夹的根目录中添加其他 htdocs 时,它显示内部错误并且根本不加载页面
    • it still shows the same url : 你用什么 URL 来测试这个?确保在测试时除了这些规则之外没有任何其他规则。
    • 我只是从 url 中删除 /public/pages/ 并对其进行测试,但它仍然重定向到相同的 URL,因为根文件夹中的 index.php 文件发送到该路径,如果我不发送到那条路径然后屏幕上什么都没有显示,它的结构与我提供的图像相同。除了你提供的,没有其他规则
    • 因此,如果您正在加载http://example.site/about,那么该 URL 不应该更改对吗?它应该只显示来自http://example.site/App/public/pages/about.php 的内容
    • 如果我尝试加载example.site/about,那么它会说在此服务器上找不到URL,如果只加载example.site,它会重定向到example.site/public/pages
    猜你喜欢
    • 2019-03-05
    • 2021-01-08
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    相关资源
    最近更新 更多