-3

Update : By the help of answers, I am able to have this update. I want following expression in my .htaccess to be working . (Complete script of .htaccess is shared in question as well)

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

Detail Of Question:

  1. I have added angularjs subsite to an exsiting expressionengine php site.
  2. subsite contains only three pages, which were very slow in existing website
  3. I want htacces to redirect those three urls to new subsite urls e.g mydomain/page2/parameter1/parameter2 to mydomain/subsite/#/page2/parameter1/parameter2 and mydomain/page2/parameter1 to mydomain/subsite/#/page2/parameter1

  4. (This is not compulsory) In subsite I want to clean up/manipulate Urls using history push state to show user mydomain/page2/parameter1/parameter2 instaed of mydomain/subsite/#/page2/parameter1/parameter2

My .htaccess is like

    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]

Following is most wanted thing to me

    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

Also this is optional, I can live if following is not achieved

In my routing.js I want to manipulate urls like

   $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
});
Community
  • 1
  • 1
Sami
  • 8,168
  • 9
  • 66
  • 99
  • Possible duplicate of [Get fragment (value after hash '#') from a URL in php](http://stackoverflow.com/questions/2317508/get-fragment-value-after-hash-from-a-url-in-php) – Jonnix Apr 26 '17 at 09:46
  • if you want to avoid appending # then you can go for HTML5 Mode instead of your hashbang mode. – Immanuel Kirubaharan Apr 26 '17 at 09:59
  • @JonStirling Wrong duplicate sir. I do not have any php file to hit for fetching requested urls. I could not find any nearly useful answer there. If you insist its duplicate then plz guide me. See edited question – Sami Apr 26 '17 at 10:08
  • @ImmanuelKirubaharanS My dear I want to add (if not exists) not remove the # from url – Sami Apr 26 '17 at 10:09
  • Then frankly I have no idea what you're asking is "possible in PHP". – Jonnix Apr 26 '17 at 10:10
  • 1
    Yes thats foolish by me. I should not mention php but apache. I am using only php services not any php page or php file to load my fornt end urls – Sami Apr 26 '17 at 10:13
  • this is certainly an apache rewrite URL question – Jason Joslin Apr 26 '17 at 10:16
  • The browser doesn't send hash in url to server – charlietfl Apr 26 '17 at 10:23
  • @Sami so basically you want to write in `apache` that it should redirect the url to that hash? in this case you could use `RewriteRule` the documentation for this is found here: https://httpd.apache.org/docs/2.0/misc/rewriteguide.html – King Reload May 11 '17 at 09:19
  • I know about that RewriteRule but unable to write that particular expression for particular urls – Sami May 11 '17 at 09:27

6 Answers6

2

Okay, Try with below rule,

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

Here you will not get the #/uri in url but you will get %23 html entity which will respond as same as # in url and your app will work.

And I am assuming you will get the way to work it out with angular.

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
  • M sorry It was not solving my problem just because I was not sure enough on requirement. See my last edit please, I m there to clarify if needed more. I will be happy to get my problem solved and award bounty – Sami May 16 '17 at 08:40
  • I've edited my answer and tell me clearly what is not working. – Abhishek Gurjar May 16 '17 at 09:14
  • What is not working. It gives me 404 error against every url request, when i added you last two lines in htaccess. Now I have added my htaccess script. plz see my edit2 to help to get it working – Sami May 16 '17 at 11:26
1

You can use mod_rewrite and php in the server side to this task


Create an 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

Add the following in your .htaccess

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

To bypass some urls aString1,aString2 you can use:

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

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

To bypass assets you can use:

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

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

Now, http://example.com/s1/s2 will redirect to http://example.com/#/s1/s2

napuzba
  • 6,033
  • 3
  • 21
  • 32
  • M sorry It was not solving my problem just because I was not sure enough on requirement. See my last edit please, I m there to clarify if needed more. I will be happy to get my problem solved and award bounty – Sami May 16 '17 at 08:40
  • Added bypass files and urls. – napuzba May 16 '17 at 09:08
0

You can use mod_rewrite and javascript snippet in the client side to this task


Add the following to .htaccess

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

Add following to main html

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

Now, http://example.com/s1/s2 will redirect to http://example.com/?s1/s2#/s1/s2

napuzba
  • 6,033
  • 3
  • 21
  • 32
0

As your question states clearly you only need your html client routes to always have # right after domain name, here is a simple way to achieve it

As you are asking this for angular, so its very easy in that case. Just paste following code in element of index.html

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

Above will include # to each url request and rest of the url will stay as it is, but only when it is requesting some front-end page

umair
  • 607
  • 4
  • 18
0

If you have access to .htaccess on your server, you can modify it to work with the URLs in your code without inserting the #.

There are really 2 parts to this, one enabling HTML5Mode in your Angularjs app and 2 editing the .htaccess file on your server to deal with this successfully.

I don't PHP at all, but was able to get this working for several sites following this article: https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/#thecode

Mike Feltman
  • 5,160
  • 1
  • 17
  • 38
0

try this:

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]

notice the comment of the first rule.

and you can try your rules in sites like this: http://htaccess.mwl.be/

am05mhz
  • 2,727
  • 2
  • 23
  • 37