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.

FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

Discussion in 'Apache' started by Nintendo, Jul 30, 2005.

  1. downloadthenet

    downloadthenet Active Member

    Messages:
    497
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #741
    That's a good question. I'm really more concerned with the forum being easy for the search engines to index than keyword friendly. I suppose the home page of the forum should be /htdv-forum without the rest of the characters. Other than that, whatever you think is easiest. Your the pro here.

    I got this from an online tool.
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule -forum-(.*)-topic-(.*)-page-(.*)\.html hdtv-forum?forum=$1&topic=$2&page=$3

    Your new URL would be: -forum-1-topic-16-page-1
    Then I found out, for Wordpress, it has to go before the WP mod_rewrite. Well, I tried it and it does nothing.
     
    downloadthenet, Sep 9, 2007 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #742
    RewriteRule ^\-forum\-(.*)\-topic\-(.*)\-page\-(.*)\.html$ hdtv-forum?forum=$1&topic=$2&page=$3 [L]

    I would get rid of the first - and hdtv-forum is probably missing a .php some where.
     
    Nintendo, Sep 10, 2007 IP
    Colleen likes this.
  3. downloadthenet

    downloadthenet Active Member

    Messages:
    497
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #743
    I tried that too. I've come to the conclusion that it doesn't matter what I put, there is simply no effect. I'm sure I have put some code that should have caused an error. It seems the rewrite is being ignored all together. The entire forum uses one wp page (not post) called hdtv-forum...hence the file name.

    Anyway, it's looking like I'll have to pay someone to figure this out. Thanks for your help. On the other hand, it looks like google is indexing the posts, so maybe I should just let it be.
     
    downloadthenet, Sep 10, 2007 IP
  4. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #744
    Hi Nintendo,

    Good to see you are still fighting strong to help all of us out. Mod rewrite will always remain french to me :)

    A new problem.

    I have a forum in a subdirectory where the home page is redirected to a particular page. It works but the url looks ugly....

    
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^.*$
    RewriteRule ^mydomain/?$ http://www.mydomain.com/vault/index.php?action=main [R=302,L]
    
    RewriteCond %{HTTP_HOST} ^.*$
    RewriteRule ^mydomain.com/index.php$ http://www.mydomain.com/vault/index.php?action=main [R=302,L]
    
    Code (markup):
    Want it to look like: http://www.mydomain.com

    and also take out the "vault" from all urls like:
    http://www.mydomain.com/vault/index.php?action=search
    http://www.mydomain.com/vault/index.php?board=2.0

    and make it like:

    http://www.mydomain.com/search
    http://www.mydomain.com/board=2.0

    Also the htaccess file is currently in the root directory for all domains. I can move that too.

    Please advice.

    Jack
     
    jack_sparrow, Sep 13, 2007 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #745
    You can't have both

    mydomain.com/search
    mydomain.com/board=2.0

    cause apache will fight over which one the URL is going to.

    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^$ vault/index.php?action=main [L]
    RewriteRule ^([^.]+)\-([^.]+)$ vault/index.php?$1=$2 [L]

    spits out

    mydomain.com/action-search
    mydomain.com/board-2.0

    and domain.com is domain.com/vault/index.php?action=main
     
    Nintendo, Sep 13, 2007 IP
  6. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #746
    Thanks, will try that.

    But the first code for redirect as mentioned in my post is in the root.... so, where do i put these....

    or

    do we combine and put all in a .htaccess in the domain root... or in the root of the subdirectory of the forum...

    Anxiously,

    Jack...
     
    jack_sparrow, Sep 13, 2007 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #747
    domain.com/.htaccess

    RewriteRule ^$ vault/index.php?action=main [L]

    makes mydomain.com look like vault/index.php?action=main

    You can't both redirect it and have it look like that or apache will go bonkers, unless if I think you have some some RewriteCond, and I'm a RewriteCond n00bie!! :eek:
     
    Nintendo, Sep 13, 2007 IP
  8. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #748
    Thanks,

    RewriteRule ^$ vault/index.php?action=main [L] - this one is working :)

    The following one isn't

    RewriteRule ^([^.]+)\-([^.]+)$ vault/index.php?$1=$2 [L]

    .... any changes or variations...

    I know about the rewritecond stuff..u told me earlier also :)

    Jack
     
    jack_sparrow, Sep 14, 2007 IP
  9. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #749
    Why not? Just match the second one first and add a "Last" flag then match the first.

    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)-(.+)$ vault/index.php?$1=$2 [C,L]
    RewriteRule ^(.+)$ vault/index.php?action=$1 [L]

    I don't often use chaining so I'm not 100% that the above will work, so if it doesn't, use this:

    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)-(.+)$ vault/index.php?$1=$2 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ vault/index.php?action=$1 [L]

    They should both do the following rewrites:

    example.com/vault/index.php?search to example.com/search
    example.com/vault/index.php?board-2.0 to example.com/board=2.0
     
    krt, Sep 14, 2007 IP
  10. jack_sparrow

    jack_sparrow Peon

    Messages:
    314
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #750
    Sorry krt,

    I tried both, but none of them are working... but thanks for your help :)

    ******************

    Nintendo !

    The first one of your codes which you gave:

    RewriteRule ^$ vault/index.php?action=main [L] is working well.

    I have removed the site redirection, now i need help for the second lots which are :

    A
    http://www.mydomain.com/vault/index.php?action=xxxxxx
    http://www.mydomain.com/vault/index.php?action=profile;u=1
    http://www.mydomain.com/vault/index.php?action=profile;u=1;sa=showPosts
    http://www.mydomain.com/vault/index.php?action=profile;u=1;sa=notification
    http://www.mydomain.com/vault/index.php?action=calendar
    http://www.mydomain.com/vault/index.php?action=usercp
    http://www.mydomain.com/vault/index.php?action=search
    http://www.mydomain.com/vault/index.php?action=search;advanced;search=
    http://www.mydomain.com/vault/index.php?action=help
    http://www.mydomain.com/vault/index.php?action=help;page=registering

    (can be many things after action)

    Just want to take out the "vault/index.php?action=" part from the url and make it something like :
    http://www.mydomain.com/xxxxx

    AND ANOTHER VARIATION - (This is in the forum part)

    B

    http://www.mydomain.com/vault/index.php?board=2.0
    http://www.mydomain.com/vault/index.php?topic=20
    http://www.mydomain.com/vault/index.php?topic=10.msg47#msg47

    Basically the structure is www.... /vault/index.php?xxxxx=
    So, I just want to take out this "/vault/index.php?xxxxx=" part fropm the URL.

    Now if A & B can be combined to one code fine, else it can be multiple.

    Thanks.


    **************************
    EDIT: THIS INFO MAY HELP YOU:

    The last time a code worked for one of my php website in the same server is given below but this only re-wrote anythign which came after index.php like index.php/xxx.


    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/
    RewriteRule ^(.*)index\.php?$ http://www.domain.com/$1 [R=301,L]


    Thought this might help
    **************************
     
    jack_sparrow, Sep 14, 2007 IP
  11. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #751
    Run over to *gags* webmasterworld.com/apache/

    The REAL apache King lives there!! :eek:
     
    Nintendo, Sep 14, 2007 IP
    onedollar likes this.
  12. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #752
    What was I thinking?? I meant:
    example.com/search to example.com/vault/index.php?action=search
    example.com/board-2.0 to example.com/vault/index.php?board=2.0

    BTW, when you copied my code, did you include a RewriteEngine On ?

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)-(.+)$ vault/index.php?$1=$2 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ vault/index.php?action=$1 [L]

    I tested the above to make sure and it is working
     
    krt, Sep 14, 2007 IP
  13. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #753
    And that's what I ment by...

    I've never figured out the RewriteCond stuff!!!
     
    Nintendo, Sep 14, 2007 IP
  14. cdilla4rilla

    cdilla4rilla Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #754
    Wow, this is pretty thorough. This is exactly what I was looking for. Thanks!
     
    cdilla4rilla, Sep 15, 2007 IP
  15. turiel

    turiel Peon

    Messages:
    148
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #755
    I've read the FAQ and tried messing about with what I'm attempting to do but I'm having no luck, so hopefully someone can help me out.

    I'm trying to do two things:
    1) Redirect www.domain.com/* to www.domain.com/dir1/subdir/$1, excluding www.domain.com/dir2 dir3 etc.
    2) Redirect *.domain.com to www.domain.com/dir1/subdir/$1, excluding www.domain.com, db.domain.com, file.domain.com.

    +Rep for a magical formula ;)
     
    turiel, Sep 25, 2007 IP
  16. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #756
    I think you would need to do RewriteCond stuff, if it's not for everything.
     
    Nintendo, Sep 25, 2007 IP
  17. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #757
    turiel:

    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/(dir1|dir2|dir3)
    RewriteRule (.*) dir1/subdir/$1 [R=301,L]

    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(.+?)\..+?\.
    RewriteCond %{HTTP_HOST} !^(www|db|file)\.
    RewriteRule (.*) dir1/subdir/%1 [L]

    If you want redirects, use [R=301,L] instead of [L]

    Also, when using *.domain.com, note that this must first be enabled on the server, e.g. by using a server alias.
     
    krt, Sep 25, 2007 IP
    turiel and Nintendo like this.
  18. turiel

    turiel Peon

    Messages:
    148
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #758
    Works like a charm, and I understand now what I was doing wrong.

    Appreciate it, thanks :)
     
    turiel, Sep 26, 2007 IP
  19. mail4kaja

    mail4kaja Peon

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
  20. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #760
    Do you have blog.mydomain.com setup as a subdomain? If not, enable it through your web host control panel or however else.

    mod_rewrite needed (in your .htaccess file):
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^blog\.
    RewriteRule (.*) http://www.mydomain.com/blog/$1 [QSA,R=301,L]
    Code (markup):
     
    krt, Oct 2, 2007 IP