Jump to content

Apache Mod_Rewrite Help


Recommended Posts

Currently my forum is in domain.com/forums and i want to remove the /forums off the url. Problem is i don't want to have to move all the files on the server as it will mean my root dir is very messy. I also want to rewrite any old urls to the new path so any old links/indexes still work.

Ive tried the following..

RewriteEngine on
RewriteRule ^index.php(.*)$ forum/index.php$1 [L]
RewriteRule ^forum/(.*)$ /$1 [R,L]

But this is causing a redirect loop.

Any ideas?

Link to comment
Share on other sites

It would appear that what you want is something like this:

RewriteEngine on

RewriteBase /


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) forum/$1 [L]


The above allows URLs with or without "forum" to point to the correct files, so old links will work as well as new ones. Of course, you'd need to alter whatever code you're using to generate the visible URLs so that they use the new URL format rather than the old format, but that's beyond the scope of mod_rewrite.

Link to comment
Share on other sites

  • 1 month later...

Got it working.


RewriteEngine On


# Externally redirect to add missing trailing slashes to directory requests

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^(.*[^/])$ http://www.yoururl.com/$1/ [R=301,L]


# Map http://www.example.com/ to /forum/ subdirectory

RewriteRule ^$ /forum/ [L]


# Map http://www.example.com/x to /forum/x unless there is a x in the web root.

RewriteCond $1 !^forum/

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /forum/$1 [L]


# check we're not already internally redirecting and 301 the old links to new location

RewriteCond %{ENV:REDIRECT_STATUS} ^$

RewriteRule ^forum/(.*)$ /$1 [R=301,NC,L]

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...