www.webdeveloper.com
Results 1 to 4 of 4

Thread: .htaccess redirect

  1. #1
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428

    Question .htaccess redirect

    I have:
    http://www.mydomain.com/mypage.php#somestring_anchor

    I would like to redirect 301 to:
    http://www.mydomain.com/mypage.php#anchor

    This is not so intricate for a single page, but I have:

    http://www.mydomain.com/mypage.php#somestring_anchor
    http://www.mydomain2.com/somestring_mypage2.php#
    http://www.otherdomain.com/mypage3.php#somestring_anchor
    ... and so on

    In other words I would like simply to remove a certain string, nomatter where it is placed, from all the possible links (thus I need a sort of wildcard or something similar with the PHP $_SERVER['PHP_SELF'], and I don't know how to build it) within my domain and redirect to the same links, but after removing that somestring_ in all of them

    Any ideas, please?
    ------
    Late note: Ok, I've seen that the anchors are not be seen by the server, thus in that case (#somestring) I had to use a PHP code. Still I wonder if Apache can perform a search/replace/rewrite job. is this possible? I've thought about something like:

    RewriteRule ^(.+)sometring_(.+)$ $1$2

    but for a reason or another it does not look to work... hm
    Last edited by Kor; 10-15-2009 at 03:53 PM.

  2. #2
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    I think I got confused by the examples... are you trying to redirect any and everything that might contain a particular "somestring" to a new location or on a case by case basis? Both are possible with .htaccess, but which are you looking at?

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Well, I've found a workaround (I am not so familiar with .htaccess, after all ).

    To put it another words: I have 4 different domains, say maindomain, domain2, domain3 and domain4 on a virtual server. Physically, I have a single site, except for some includes which makes the four "indexes" slightly different and give different values to the document's title and meta tags for keywords, according to each of those domains.

    Now, my boss wanted 2 extra things: To insert some keywords inside the menu's links, each domains with his own keyword, but when click on those links, the user should be 301 redirect to the correspondent maindomain's (in other words, to redirect 301 to a link from which I should remove that keyword.

    Everything works under php/MySQL on Apache.

    To simplify, let's take the case of the contact.php link. On those 4 domains, in their indexes, the links towards that contact page should look like:

    http://maindomain/keyword0_contact.php
    http://domain2/keyword1_contact.php
    http://domain3/keyword2_contact.php
    http://domain4/keyword3_contact.php

    Now, from no matter the domain's indexes, those links should redirect, all of them, to the unique file:

    http://maindomain/contact.php

    My solution so far (it works) is:
    Code:
    RewriteEngine on 
    
    RewriteRule ^(.*)_contact http://maindomain/contact.php [R=301,L]
    
    # and repeat the line for all the other menu's links (tariffs, skills...)
    
    RewriteRule ^(.*)_tariffs http://maindomain/tariffs.php [R=301,L]
    RewriteRule ^(.*)_skills http://maindomain/skills.php [R=301,L]
    
    # ...
    Thankfully I have only 12 pages to be linked from the menu, thus I wrote 12 code lines.
    I was only wondering if I could have refined the code in a more dynamical way
    Last edited by Kor; 10-19-2009 at 10:33 AM.

  4. #4
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    OK, but I would change the links in the menu also (assuming the change IS permanent)... or another option would be to pass the domain/keyword variable to the new "main" contact form so that you know where the request is coming from... http://maindomain/contact.php?domain2_keyword either from the menu or/and the .htaccess. (you can convert static urls to dynamic urls also).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles