1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Htaccess Redirect rule help needed

Discussion in 'Programming' started by expertscolumn, Aug 17, 2013.

  1. #1
    we are developing subdomains and we want to redirect following dynamic url to static:
    Let' say main domain name is http://example.com and sub domain name is sub1.example.com. The URLs which are being getting created are in the format sub1.example.com?index12.php?id=1.

    Now how should we redirect sub1.example.com?index12.php?id=1 to sub1.example.com/abc-def-ghi permanently(301) for any individual page where "ABC DEF GHI" is a name (title) given to an individual page.

    Please post an exact redirect rule needed for this permanent redirection
     
    expertscolumn, Aug 17, 2013 IP
  2. EmmanuelFlossie

    EmmanuelFlossie Active Member

    Messages:
    159
    Likes Received:
    11
    Best Answers:
    2
    Trophy Points:
    65
    #2
    Don't use redirect but rewrite rule.

    Depending on what you need the following can be used. But for you to better understand or research what you want just google htaccess rewrite query string

    
    Options+FollowSymlinksRewriteEngineOn# specific ruleRewriteCond%{QUERY_STRING}^([^=]+)=([^&]+)RewriteRule^(constant)$ /index.php?url=$1&type=%1&task=%2[L]# general catch-allRewriteCond%{REQUEST_FILENAME}!-f
    RewriteCond%{REQUEST_FILENAME}!-d
    RewriteRule^(.*)$ index.php?url=$1 [L]
    
    Code (markup):
    or

    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    Code (markup):
     
    EmmanuelFlossie, Aug 18, 2013 IP
    akselsson likes this.
  3. akselsson

    akselsson Member

    Messages:
    19
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    28
    #3
    I believe WebStumblr's second example is used in Wordpress .htaccess file. It's good if you have many pages - indeed it'd be pain to hard-code all of your hundred pages to .htaccess. Anyway, the above solution reroutes visitors requests to index.php. Thus, if you use the above solution, you should also write a URL handler to index.php that captures the URL client requested and fetches the correct code from database. It's effective way yet perhaps not the best if you have, say, just 5 pages.

    In the case of having just a handful of pages, you can hard-code everything to .htaccess. Like this:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^my_page_name/?$ index.php?id=1 [L,QSA]
    RewriteRule ^my_other_page_name/?$ index.php?id=2 [L,QSA]
    RewriteRule ^my_third_page_name/?$ index.php?id=3 [L,QSA]
    Code (markup):
    Now, when you go to sub1.example.com/my_page_name/, contents of sub1.example.com/index.php?id=1 are actually shown while sub1.example.com/my_page_name/ is the URL shown in the address bar.

    Hope this helps.
     
    akselsson, Aug 18, 2013 IP