dynamic url redirection via htaccess

16 replies
Hello Warriors,

I really need help please.

I need to redirect this URL using htacess:

From this: http ://example.com/city/Chennai/search/search_display_event_type/category1

To this: http ://example.com/chennai/category1

Is there any way we can write in htacees that will redirect like below url http ://example.com/(chennai | pune | mumbai)/category1

I know this is pretty hard, but I'm really not sure what type of rule would work for this. because I dont want to write same lines of code for just city change, so i want something like array match in htaccess.

Any help is greatly appreciated!
#dynamic #htaccess #redirection #url
  • Profile picture of the author quickscrum
    Hello jhakasseo,

    Please follow this url to make your changes How do I redirect my site using a .htaccess file?
    {{ DiscussionBoard.errors[9373402].message }}
  • Profile picture of the author Kasparas
    Using something like this in your .htaccess.
    Code:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^city/(.*)/search/search_display_event_type/(.*)$ http://example.com/$1/$2 [R=301,L]
    {{ DiscussionBoard.errors[9373414].message }}
    • Profile picture of the author jhakasseo
      Originally Posted by Kasparas View Post

      Using something like this in your .htaccess.
      Code:
      Options +FollowSymLinks
      RewriteEngine On
      RewriteRule ^city/(.*)/search/search_display_event_type/(.*)$ http://example.com/$1/$2 [R=301,L]
      This is what I currently have in htaccess file
      dynamic redirection - Pastebin.com

      on website there are more than 3000 urls that need to change from old to new.
      so I am looking any dynamic solution for same.
      Every city has 19 category pages.
      so city + category 1, city + category 2 and the list goes on...
      I want to redirect old urls to new one.
      Or what will be the best way to do this?
      Signature
      Real Youtube & Instagram Promotion Click Here
      {{ DiscussionBoard.errors[9373690].message }}
    • Profile picture of the author minirich
      Originally Posted by Kasparas View Post

      Using something like this in your .htaccess.
      Code:
      Options +FollowSymLinks
      RewriteEngine On
      RewriteRule ^city/(.*)/search/search_display_event_type/(.*)$ http://example.com/$1/$2 [R=301,L]
      If you are using the code from Kasparas
      You can remove the piece of code in line 3 and 4 and replace it with the RewriteRule above. It is dynamic.
      If you want to use 301 permanent redirect, this is not for idividual urls on a website.
      you only write one 301 for the entire website which will then redirect to a new domain.

      Code:
      Options +FollowSymLinks
      RewriteEngine On
      RewriteRule ^city/(.*)/search/search_display_event_type/(.*)$ http://example.com/$1/$2 [R=301,L]
      The extra part with
      Code:
      http://example.com
      is unnecessary as long as you stay on the same domain.

      Hope that helps
      Mike
      {{ DiscussionBoard.errors[9378225].message }}
  • Profile picture of the author jhakasseo
    @minirich

    RewriteEngine on

    RewriteRule ^city/(.*)$/search/search_display_event_type/(.*)$ http://www.example.org/$1/$2 [R=301,L]

    this is not working and it is going to 404 page?
    Signature
    Real Youtube & Instagram Promotion Click Here
    {{ DiscussionBoard.errors[9379210].message }}
    • Profile picture of the author minirich
      Have you tried to remove the http part and starting with a /
      Like that:
      Code:
      RewriteRule ^city/(.*)$/search/search_display_event_type/(.*)$ /$1/$2 [R=301,L]
      {{ DiscussionBoard.errors[9379309].message }}
      • Profile picture of the author jhakasseo
        Originally Posted by minirich View Post

        Have you tried to remove the http part and starting with a /
        Like that:
        Code:
        RewriteRule ^city/(.*)$/search/search_display_event_type/(.*)$ /$1/$2 [R=301,L]
        Actually its working with above mention code but the problem is there is multiple cities on same structure of domain but the categories are same. However new urls I am trying to do is:

        Code:
        RewriteRule ^city/(.*)$/search/search_display_event_type/meeting(.*)$ /$1/$2 [R=301,L]
        
        To RewriteRule ^city/city1|city|2|city3(.*)$/search/search_display_event_type/meeting-rooms(.*)$ /$1/$2 [R=301,L]
        There are multiple categories also like meeting rooms which was mentioned as only "meetings" in old domain. Here is the main website if you like to see which are we working to change url structure example and this is how its going to look exampl.org

        We are directing for search engines because there are so many webpages got indexed and want them to redirect to example.org this kind of urls.
        Signature
        Real Youtube & Instagram Promotion Click Here
        {{ DiscussionBoard.errors[9379684].message }}
        • Profile picture of the author minirich
          Ok i had a look on the page and all the possible url combinations.
          This is a bit more complicated than you told in the beginning.

          the structure of your urls look like that:
          Code:
          http://example.com/city/(city)/search/search_display_event_type/(Category)/(Location)/(Capacity)/(Configuration)
          while city can by any of 8 specified
          Category is one of these: Wedding+Reception, Meetings, Conference, Brithday-Party, Private-Party
          Location: depends on the city and can be a whole lot to choose from
          Capacity: a range in the form of low-high
          Configuration: the arrangement of the seating can be one of 7 (Conference-Style, U-Shape, Holow-Square-style, Theater-style, Classroom-style, open, cluster)

          while city and category are mandatory to see any result, Location, Capacity and Configuration are optional but will appear in a specific order.
          The following rules will be the result: the order is important.
          Code:
          RewriteBase /
          # just the city
          RewriteRule ^(.*)$ /city/$1 [NC, L] 
          # city with category
          RewriteRule ^(.*)/(.*)$ /city/$1/search/search_display_event/$2 [NC, L]
          # city with category and Configuration
          RewriteRule ^(.*)/(.*)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3 [NC,L]
          # city with category and Capacity
          RewriteRule ^(.*)/(.*)/([d]+-[d]+)$ /city/$1/search/search_display_event/$2/$3 [NC,L]
          # city with category and location
          RewriteRule ^(.*)/(.*)/([D-]*)$ /city/$1/search/search_display_event/$2/$3 [NC,L]
          # city with category Location  and Configuration
          RewriteRule ^(.*)/(.*)/([D-]*)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3/$4 [NC,L]
          # city with category capacity and configuration
          RewriteRule ^(.*)/(.*)/([d]+-[d]+)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3/$4 [NC,L]
          # city with category location and capacity
          RewriteRule ^(.*)/(.*)/([D-]*)/([d]+-[d]+)$ /city/$1/search/search_display_event/$2/$3/$4 [NC,L]
          # city with category location capacity and configuration
          RewriteRule ^(.*)/(.*)/([D-]*)/([d]+-[d]+)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3/$4/$5 [NC,L]
          These rules should be able to handle all the following url permuations:
          Code:
          http://example.com/Mumbai
          http://example.com/Mumbai/Meetings
          http://example.com/Mumbai/Meetings/Cluster
          http://example.com/Mumbai/Meetings/1-500
          http://example.com/Mumbai/Meetings/Dadar
          http://example.com/Mumbai/Meetings/Dadar/Cluster
          http://example.com/Mumbai/Meetings/Dadar/1-500
          http://example.com/Mumbai/Meetings/1-500/Cluster
          http://example.com/Mumbai/Meetings/Dadar/1-500/Cluster
          I hope i did not add any nasty typos. (It is already late).
          And if some one out there knows a way to optimize this feel free to comment.

          Hope that helps
          Mike
          {{ DiscussionBoard.errors[9381116].message }}
          • Profile picture of the author jhakasseo
            Originally Posted by minirich View Post

            Ok i had a look on the page and all the possible url combinations.
            This is a bit more complicated than you told in the beginning.

            the structure of your urls look like that:
            Code:
            http://example.com/city/(city)/search/search_display_event_type/(Category)/(Location)/(Capacity)/(Configuration)
            while city can by any of 8 specified
            Category is one of these: Wedding+Reception, Meetings, Conference, Brithday-Party, Private-Party
            Location: depends on the city and can be a whole lot to choose from
            Capacity: a range in the form of low-high
            Configuration: the arrangement of the seating can be one of 7 (Conference-Style, U-Shape, Holow-Square-style, Theater-style, Classroom-style, open, cluster)

            while city and category are mandatory to see any result, Location, Capacity and Configuration are optional but will appear in a specific order.
            The following rules will be the result: the order is important.
            Code:
            RewriteBase /
            # just the city
            RewriteRule ^(.*)$ /city/$1 [NC, L] 
            # city with category
            RewriteRule ^(.*)/(.*)$ /city/$1/search/search_display_event/$2 [NC, L]
            # city with category and Configuration
            RewriteRule ^(.*)/(.*)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3 [NC,L]
            # city with category and Capacity
            RewriteRule ^(.*)/(.*)/([d]+-[d]+)$ /city/$1/search/search_display_event/$2/$3 [NC,L]
            # city with category and location
            RewriteRule ^(.*)/(.*)/([D-]*)$ /city/$1/search/search_display_event/$2/$3 [NC,L]
            # city with category Location  and Configuration
            RewriteRule ^(.*)/(.*)/([D-]*)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3/$4 [NC,L]
            # city with category capacity and configuration
            RewriteRule ^(.*)/(.*)/([d]+-[d]+)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3/$4 [NC,L]
            # city with category location and capacity
            RewriteRule ^(.*)/(.*)/([D-]*)/([d]+-[d]+)$ /city/$1/search/search_display_event/$2/$3/$4 [NC,L]
            # city with category location capacity and configuration
            RewriteRule ^(.*)/(.*)/([D-]*)/([d]+-[d]+)/(Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)$ /city/$1/search/search_display_event/$2/$3/$4/$5 [NC,L]
            These rules should be able to handle all the following url permuations:
            Code:
            http://example.com/Mumbai
            http://example.com/Mumbai/Meetings
            http://example.com/Mumbai/Meetings/Cluster
            http://example.com/Mumbai/Meetings/1-500
            http://example.com/Mumbai/Meetings/Dadar
            http://example.com/Mumbai/Meetings/Dadar/Cluster
            http://example.com/Mumbai/Meetings/Dadar/1-500
            http://example.com/Mumbai/Meetings/1-500/Cluster
            http://example.com/Mumbai/Meetings/Dadar/1-500/Cluster
            I hope i did not add any nasty typos. (It is already late).
            And if some one out there knows a way to optimize this feel free to comment.

            Hope that helps
            Mike
            Hello Mike,

            Thank you very much for taking time to see website and replying. I really appreciate.

            The one thing that I also forget to add that the category like meetings etc going to change from meetings to meeting-halls, from wedding+reception to wedding-reception-halls etc.

            So this is also I want to add in htaccess with above mention solution. Need help on this actually.

            The actual urls look likes as below with all lowercase letters.
            Code:
            http://www.example.com/mumbai
            http://www.example.com/mumbai/meeting-halls
            http://www.example.com/mumbai/meeting-halls/cluster
            http://www.example.com/mumbai/meeting-halls/1-500
            http://www.example.com/mumbai/meeting-halls/dadar
            http://www.example.com/mumbai/meeting-halls/dadar/cluster
            http://www.example.com/mumbai/meeting-halls/dadar/1-500
            http://www.example.com/mumbai/meeting-halls/1-500/cluster
            http://www.example.com/mumbai/meeting-halls/dadar/1-500/cluster
            I know I already ate lots of your precious time but you have been a very helpful for me till the time. I really appreciate that.
            Signature
            Real Youtube & Instagram Promotion Click Here
            {{ DiscussionBoard.errors[9381961].message }}
  • Profile picture of the author minirich
    It should work for any kind of category. The rules contain a regular expression (.*) this will match every character.
    The only thing which needs to be changed is if you change to configuration values.
    (Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)
    if you have your new configuration names. just search the old name and replace it with the new name.

    Just send me a pm or skype me when you need help there too.
    Mike
    {{ DiscussionBoard.errors[9382723].message }}
    • Profile picture of the author jhakasseo
      Originally Posted by minirich View Post

      It should work for any kind of category. The rules contain a regular expression (.*) this will match every character.
      The only thing which needs to be changed is if you change to configuration values.
      (Conference-Style|U-shape|Holow-square-style|Theater-style|Classroom-style|Open|Cluster)
      if you have your new configuration names. just search the old name and replace it with the new name.

      Just send me a pm or skype me when you need help there too.
      Mike
      It should work for any kind of category. The rules contain a regular expression (.*) this will match every character.
      This is the main problem we are also not matching every character hence there are changes in some characters while redirecting. means the category pattern dont match with new category. They are new keywords in url. but the content they will show from old url. meetings to meeting hall.
      How to identify and put non match query to get it redirect.
      Signature
      Real Youtube & Instagram Promotion Click Here
      {{ DiscussionBoard.errors[9385156].message }}
      • Profile picture of the author minirich
        If it is not possible to use the values directly in the redirect we have to use a rewrite mapping.

        The problem i have is, i'm the next 8 to 10 days offline.
        If you need it really urgent i will try to do it today.
        But i need a list of the values and to what they should be mapped to.

        Mike
        {{ DiscussionBoard.errors[9385286].message }}
        • Profile picture of the author jhakasseo
          Originally Posted by minirich View Post

          If it is not possible to use the values directly in the redirect we have to use a rewrite mapping.

          The problem i have is, i'm the next 8 to 10 days offline.
          If you need it really urgent i will try to do it today.
          But i need a list of the values and to what they should be mapped to.

          Mike
          Hello Mike thank you... Above solution really help me and it worked..

          Now I want to redirect
          from http:// xyz.com/city/Pune/welcome/venue_display/Abc-Xyz/Locality to http:// www. xyz.com/pune/abc-xyz/locality

          I want some part of url in lowercase example: (Abc-Xyz/Locality to abc-xyz/locality)

          So is there any rewrite rule or anything that I want to mention in htaccess for this specific?
          Signature
          Real Youtube & Instagram Promotion Click Here
          {{ DiscussionBoard.errors[9393186].message }}
          • Profile picture of the author minirich
            Hi,
            sorry for answering now, i extended the vacation, because my wife and kids wanted to burry their feet in the beach sand for another week.

            There is a way to sort the uppercase problem in the redirect rules, but i have to research it a bit, because currently i belive it is only working in the virtual host configuration.

            Have you got the list for the category substitutions (meeting -> meeting-halls) ready, i will try to fix this in one big swoop.

            Mike
            {{ DiscussionBoard.errors[9435153].message }}
            • Profile picture of the author jhakasseo
              Originally Posted by minirich View Post

              Hi,
              sorry for answering now, i extended the vacation, because my wife and kids wanted to burry their feet in the beach sand for another week.

              There is a way to sort the uppercase problem in the redirect rules, but i have to research it a bit, because currently i belive it is only working in the virtual host configuration.

              Have you got the list for the category substitutions (meeting -> meeting-halls) ready, i will try to fix this in one big swoop.

              Mike
              Hello Mike,

              Yeah good to see you back..

              Finally I found the way to solve all issues with combination of htaccess and config file from hosting. Redirected all old urls to new with lowercase.

              So many thanks to you bro..
              Signature
              Real Youtube & Instagram Promotion Click Here
              {{ DiscussionBoard.errors[9435249].message }}
              • Profile picture of the author minirich
                Great, so your happy?

                Mike
                {{ DiscussionBoard.errors[9436537].message }}

Trending Topics