【问题标题】:Redirect URLs with specific pattern to same URL in subsite将具有特定模式的 URL 重定向到子站点中的相同 URL
【发布时间】:2017-04-26 09:39:13
【问题描述】:

更新:在答案的帮助下,我能够获得此更新。我希望 .htaccess 中的以下表达式能够正常工作。 (.htaccess 的完整脚本也有问题共享)

    RewriteCond %{REQUEST_URI} ((mypage2(/\d{4}-\d{2}-\d{2})?/\d{1,2}) |about-us)
    RewriteRule ^(.+)$ /subsite/#/$1 [R=301,NC]

问题详情:

  1. 我已将 angularjs 子站点添加到现有的表达式引擎 php 站点。
  2. 子网站只有三个页面,在现有网站中非常慢
  3. 我希望 htacces 将这三个 url 重定向到新的子站点 url,例如 mydomain/page2/parameter1/parameter2 到 mydomain/subsite/#/page2/parameter1/parameter2 和 mydomain/page2/parameter1 到 mydomain/subsite/# /page2/parameter1

  4. (这不是强制性的)在子站点中,我想使用history push state 清理/操作 URL 以显示用户 mydomain/page2/parameter1/parameter2 代替 mydomain/subsite/#/page2/parameter1/parameter2

我的 .htaccess 就像

    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d       
    RewriteRule ^(.*)$ /index.php?/#/$1 [L,QSA]

关注是我最想要的东西

    RewriteCond %{REQUEST_URI} ((mypage2(/\d{4}-\d{2}-\d{2})?/\d{1,2}) |about-us)
    RewriteRule ^(.+)$ /subsite/#/$1 [R=301,NC] ==> when url is like mypage2(/date optional)/pagenumber then redirect it to subsite/#/mypage2(/date optional)/pagenumber

还有这个是可选的,如果没有达到我可以活

在我的 routing.js 中,我想像这样操作 url

   $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams,
 fromState, fromParams, options, Data) { 
     var cleanUrl = window.location.toString().replace(subsite+'/#/', '');           
    window.history.pushState(null, null, cleanUrl);
    //alert("Yes this works, it shows me the required url in browser at this moment");
   //Here I am trying to clean the url
   //But after that it reloads, some unwanted redirection happens after it
});

【问题讨论】:

  • 如果你想避免附加 # 那么你可以选择 HTML5 模式而不是你的 hashbang 模式。
  • @JonStirling 错误的重复先生。我没有任何 php 文件来获取请求的 url。我在那里找不到任何几乎有用的答案。如果您坚持要复制,请指导我。查看已编辑的问题
  • @ImmanuelKirubaharanS 亲爱的,我想添加(如果不存在)而不是从 url 中删除 #
  • 是的,我觉得这很愚蠢。我不应该提到 php,而是 apache。我只使用 php 服务而不是任何 php 页面或 php 文件来加载我的前端 url

标签: .htaccess url-rewriting url-redirection


【解决方案1】:

好的,试试下面的规则,

RewriteEngine On
RewriteCond %{REQUEST_URI} ^!(aSpecialString2|aSpecialString1|/)
RewriteRule ^(.+)$ /#/$1 [R=301,NC]

在这里,您不会在 url 中获得 #/uri,但您将获得 %23 html 实体,该实体的响应与在 url 中的 # 相同,您的应用程序将正常运行。

我假设你会找到用 angular 解决它的方法。

【讨论】:

  • 对不起,这并没有解决我的问题,只是因为我对要求不够确定。请参阅我的最后一次编辑,如果需要更多,我会澄清。我很乐意解决我的问题并奖励赏金
  • 我已经编辑了我的答案并清楚地告诉我什么不起作用。
  • 什么不起作用。当我在 htaccess 中添加最后两行时,它对每个 url 请求都给出了 404 错误。现在我已经添加了我的 htaccess 脚本。请查看我的 edit2 以帮助其正常工作
【解决方案2】:

你可以在服务器端使用 mod_rewrite 和 php 来完成这个任务


创建一个 index.php

<?php
    if ( !empty($_GET['id']) ) {
      $protocol = 'http://';    
      if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
         $protocol = 'https://';
      } 
      header('Location:' . $protocol . $_SERVER["HTTP_HOST"]. '/#/' . $_GET[id]);
      exit();
    }
?>
You html here

在您的 .htaccess 中添加以下内容

RewriteEngine On
RewriteBase /
RewriteRule "index.php"  - [L]
RewriteRule "^(.*)$"  "index.php?id=$1"

要绕过一些网址aString1,aString2,您可以使用:

RewriteEngine On
RewriteBase /
RewriteRule "index.php"  - [L]

RewriteCond %{REQUEST_URI} !^(aString1) 
RewriteCond %{REQUEST_URI} !^(aString2) 
RewriteRule "^(.*)$"  "index.php?id=$1"

绕过你可以使用的资产:

RewriteEngine On
RewriteBase /
RewriteRule "index.php"  - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^(.*)$"  "index.php?id=$1"

现在,http://example.com/s1/s2 将重定向到 http://example.com/#/s1/s2

【讨论】:

  • 对不起,这并没有解决我的问题,只是因为我对要求不够确定。请参阅我的最后一次编辑,如果需要更多,我会澄清。我很乐意解决我的问题并奖励赏金
  • 添加绕过文件和网址。
【解决方案3】:

您可以在客户端使用 mod_rewrite 和 javascript sn-p 来完成此任务


将以下内容添加到 .htaccess

RewriteEngine On
RewriteBase /
RewriteRule "^([^?#].*)$"  "/?$1" [R=301,NC]

在主 html 中添加以下内容

  <script>
  window.onload = function() {  
    location.hash = '/' + location.search.substr(1)  
  }
  </script>

现在,http://example.com/s1/s2 将重定向到 http://example.com/?s1/s2#/s1/s2

【讨论】:

    【解决方案4】:

    正如你的问题清楚地表明你只需要你的 html 客户端路由总是在域名之后有 #,这是一个简单的方法来实现它

    由于您要求这个角度,所以在这种情况下它很容易。只需将以下代码粘贴到 index.html 的元素中

    <script>
           var currentSitePageUrl = window.location.toString(); 
           if(currentSitePageUrl.indexOf('#/') == -1)
           {
               window.location = currentSitePageUrl.replace(yourDomainUrl+'#/')
           }
       }
    </script>
    

    上面将在每个 url 请求中包含 # ,其余 url 将保持原样,但仅在请求某些前端页面时

    【讨论】:

      【解决方案5】:

      如果您有权访问服务器上的 .htaccess,则可以修改它以使用代码中的 URL,而无需插入 #。

      这实际上分为两部分,一是在您的 Angularjs 应用程序中启用 HTML5Mode,二是在您的服务器上编辑 .htaccess 文件以成功处理此问题。

      我根本不会使用 PHP,但能够在本文之后的几个网站上使用它:https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/#thecode

      【讨论】:

        【解决方案6】:

        试试这个:

        RewriteEngine On
        RewriteBase /
        
        RewriteCond %{REQUEST_URI} /(mypage2(/.*)|about-us)   # you used mypage2, so it will be redirected to subsite/#/mypage2
        RewriteRule ^(.+)$ /subsite/#/$1 [R=301,NC]
        
        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
        
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
        RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
        
        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d       
        RewriteRule ^(.*)$ /index.php?/#/$1 [L,QSA]
        

        注意第一条规则的注释。

        您可以在这样的网站中尝试您的规则:http://htaccess.mwl.be/

        【讨论】:

          猜你喜欢
          • 2018-02-15
          • 2017-02-20
          • 2014-05-04
          • 1970-01-01
          • 2022-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-19
          相关资源
          最近更新 更多