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.

mod_rewrite help

Discussion in 'Site & Server Administration' started by Jason S, May 30, 2016.

  1. #1
    Hi all! I am looking to add some mod_rewrite rules to my website but — like many people — am totally confused by htaccess and mod_rewrite. Can anyone please help me write my rules? My current code is:

    <IfModule mod_rewrite.c>
        RewriteEngine On
       RewriteCond %{HTTP_HOST} www.comparewear.com$
        RewriteRule ^(.*)$ http://comparewear.com/$1 [R=301,L]   
    </IfModule>
    Code (markup):
    The rules I would like are:

    1. If the URL is
    /wearables/?vs=productA
    (where there is only one 'vs' parameter)
    303 redirect to
    /product/productA/

    2. If the URL is
    /wearables/?vs=productA&vs=productB&vs=productC
    (where there are 2 OR MORE 'vs' parameters)
    303 redirect to
    /compare/productA-vs-productB-vs-productC/

    3. /product/productA/
    actually loads
    /product.php?vs=productA

    4. /compare/productA-vs-productB-vs-productC/
    actually loads
    /compare.php?vs=productA&vs=productB&vs=productC

    Thank you very much!!
     
    Jason S, May 30, 2016 IP
  2. Roger S

    Roger S Active Member

    Messages:
    46
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    58
    #2


    Bro, your requirement needs serious formatting. If you use the above formats in .htaccess the site will have serious redirection problem and will end up in redirect loop which is a bad idea.

    What platform are you using if you use a CMS it will be easy to create uRLs and you do not need .htaccess redirection..

    From what i can understand is that you may have plans to change the SEO links in your existing links and as if you directly URLS you will loose the SEO rankings.

    Anyways, the above can be achieved via mod_redirect and mod_proxy passthrough instances.

    https://httpd.apache.org/docs/current/mod/mod_proxy.html


    To create .htaccess rules use this if you wish.
    http://www.webconfs.com/htaccess-redirect-generator.php

    Hope that helps.
     
    Roger S, Jul 11, 2016 IP
  3. Jason S

    Jason S Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    13
    #3
    Haha. Thanks. I actually worked out a solution as follows. You're right that I am running into some sort of redirect loop under certain unusual circumstances, but generally this works just fine. The issue I'm having is that I can't use Google Analytics experiments via the client-side, javascript redirect method. The client-side redirect must somehow be interfering with the server-side htaccess redirects, and I don't know why. Any thoughts?
    
    <IfModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
       RewriteCond %{HTTP_HOST} www.comparewear.com$
       RewriteCond %{SCRIPT_FILENAME} !-d
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteRule ^(.*)$ http://comparewear.com/$1 [R=301]
        RewriteRule ^smartwatches/(.*)$ ./product.php [L]
        RewriteRule ^fitness-trackers/(.*)$ ./product.php [L]
        RewriteRule ^compare/(.*)$ ./comparison.php [L]
    </IfModule>
    Code (markup):
    /wearables/index.php:
    
    . . .
    header("HTTP/1.1 301 Moved Permanently"); 
    header('Location: http://comparewear.com/'.$URL_friendly);
    Code (markup):
     
    Jason S, Jul 11, 2016 IP
  4. Roger S

    Roger S Active Member

    Messages:
    46
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    58
    #4


    Buddy, I am not a developer anyways I am more of a server administrator actually.
    Anyways I will try my best to explain. The .htaccess rules can be a bit tricky when custom rules come into picture.

    The rules that you added in the code box ahead truncates the 2nd part as I guess. Like you stop at compare, product.php pages are it something that needs consideration.

    I usually test redirections via Linux shells using Curl commands and that helps seriously.

    Command that I use is curl - I "URL that needs testing"
    you will get a response and that helps a lot when you run into redirection problems.

    Keep in mind that .htaccess redirections are slower to render as each time a page loads apache/Litespeed/what ever web server has to process them individually so can also affect load times and site performances. This is why I was avoiding .htacces redirection.

    Anyways, you know better than me so try them out and let me know if you run into some trouble. :)
     
    Roger S, Jul 11, 2016 IP