Forum Moderators: phranque

Message Too Old, No Replies

Virtual folder with mod rewrite

         

Mark26

10:56 am on Aug 22, 2010 (gmt 0)

10+ Year Member



My forum is located at www.domain.tld and I would like to have it in a virtal folder at www.domain.tld/key1-key2-key3/ or www.domain.tld/key1/key2/key3/ if that is better SEO-wise.

I've searched really long on Google, but couldn't get it done. Any help greatly appreciated!

jdMorgan

1:24 pm on Aug 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



  1. Modify the forum script to publish the desired URLs on every page that it generates. This is the only way to "change the URL," because URLs are defined and created by links.

  2. Write an internal-rewrite rule to go in your .htaccess file to tell the server that when it sees a request for /key1-key2-key3, it should pass that request to the forum script at /forum.php (or whatever the correct forum-script filepath is).

  3. Write an external redirect rule to redirect *only* direct client requests for /forum.php to http://www.example.com/key1-key2-key3

Three steps -- the first two required, and the last optional. Only the first step is potentially difficult. If you are not comfortable with modifying your forum script code, look into purchasing a "plug-in" or add-on for your forum script to modify the published URLs. This latter approach is often cheaper over the long run, as you will not have to re-modify the forum script every time it gets a security or feature update...

Jim

Mark26

3:01 pm on Aug 22, 2010 (gmt 0)

10+ Year Member



Thanks for your answer!

To make things easier I have now physically created the directory "key1-key2-key3", moved the forum files into it and changed two forum settings. Now I can access it via www.domain.tld/key1-key2-key3/

I have to admit that I am a real newbie when it comes to mod_redwrite. I want to 301 redirect www.domain.tld/ to www.domain.tld/key1-key2-key3/ and also www.domain.tld/key1-key2-key3/index.php to www.domain.tld/key1-key2-key3/

I know this should be really easy, but I'm stuck after 3 hours of trying to achieve both. *ashamed*

What would be the smartest solution? Thanks in advance!

Mark26

4:49 pm on Aug 22, 2010 (gmt 0)

10+ Year Member



That's what I have right now... doesn't work. :-(


RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_HOST} ^www.domain.tld
RewriteRule (.*) http://www.domain.tld/key1-key2-key3/$1 [R=301,L]

RewriteBase /key1-key2-key3
RewriteRule ^index\.php$ http://www.domain.tld/key1-key2-key3/? [R=301,L,NC]

jdMorgan

5:05 pm on Aug 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try searching this forum for keywords similar to "rewrite folder," "rewrite subdirectory," "virtual folder rewrite," etc. Also have a look at our Apache Forum Library and at the resources cited in our Forum Charter (both links are above in this page's header).

The code is fairly trivial, but you cannot "guess at it." Even code that "appears to work" can have side-effects that can be disastrous for your site's performance and search rankings...

Jim

Mark26

9:57 pm on Aug 22, 2010 (gmt 0)

10+ Year Member



It took me long to find just one line that makes the 301 redirect to the folder I created. Would it be possible that you confirm it's correct please?

RedirectMatch permanent ^/$ http://www.domain.tld/key1-key2-key3/


I still couldn't figure out how to solve the duplicate content problem by redirecting from www.domain.tld/key1-key2-key3/index.php to www.domain.tld/key1-key2-key3/ :-(

Here is what I have:

RewriteEngine on
RewriteBase /key1-key2-key3
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ www.domain.tld/key1-key2-key3/ [R=301,L,NC]


The .htaccess-file is located in the www-folder.

jdMorgan

10:29 pm on Aug 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't mix mod_alias with mod_rewrite:

RewriteEngine on
#
# Redirect direct client requests for /key1-key2-key3/index\.php with
# any appended queries or fragments to www.example.com/key1-key2-key3/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /key1-key2-key3/index\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^key1-key2-key3/index\.php$ http://www.example.com/key1-key2-key3/ [R=301,L]
#
# Redirect old keyword-less "home page" requests to new keyword-rich virtual-subdirectory URL
RewriteRule ^(index\.php)?$ http://www.example.com/key1-key2-key3/ [R=301,L]

Jim

Mark26

11:27 pm on Aug 22, 2010 (gmt 0)

10+ Year Member



Thank you! Works fine. Just http://www.example.com/key1-key2-key3/index.php is still not being redirected to http://www.example.com/key1-key2-key3/ which would prevent duplicate content.

jdMorgan

2:18 pm on Aug 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Common problems/troubleshooting:

  • Did you delete your browser cache before testing any new code? You must do so to avoid your browser showing you stale cached pages and server responses.

  • Do you have any other preceding rules which might be interfering with the "direct client requests" rule?

  • Disable MultiViews and AcceptPathInfo unless your site requires them.

  • Check the regular-expressions pattern against what you are actually typing into the address bar. I assume that "key1-key2-key3/" is a "stand-in" for the actual URL-path for purposes of discussion here; The regex pattern in the RewriteCond and the RewriteRule must match the actual requested URL-path exactly, including character-case and trailing slash.

  • It is often quite useful to observe the client/server transactions using the Live HTTP Headers add-on for Firefox and other Mozilla-based browsers. If an incorrect redirect or a redirection chain is being invoked, this tool (or one like it) will make that quite obvious.

    Jim
  • Mark26

    4:40 pm on Aug 23, 2010 (gmt 0)

    10+ Year Member



    I already got it to work:

    RewriteEngine On
    RewriteRule ^([^/]*)$ http://www.domain.tld/key1-key2-key3/$1 [R=301,L]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /key1-key2-key3/index\.php\ HTTP/
    RewriteRule ^key1-key2-key3/index\.php$ http://www.domain.tld/key1-key2-key3/ [R=301,L,NC]


    Thank you very much!

    g1smd

    9:49 pm on Aug 23, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    I believe that request for example.com/index.php will see an unwanted double redirect, a Redirection Chain.

    You will also need to add your non-www to www canonicalisation rules here.