13

What is the best method to rewrite anything below "/some/subdir" to "/some/subdir/projects" like from this:

http://www.mydomain.com/some/subidr/test/

... to this:

http://www.mydomain.com/some/subdir/projects/test/

I found a similar question posted, but the solution didn't seem to work in my case. My current attempt so far (which doesn't seem to work):

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/projects/.*$
RewriteRule ^(.*)$ /projects/$1 [L]

EDIT: I forgot to mention the .htaccess file would have to be sitting within /some/subdir as I don't have write access to the server's web root.

Community
  • 1
  • 1
Wilco
  • 32,754
  • 49
  • 128
  • 160

3 Answers3

13

This is the solution I finally got to work:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/some/subdir/projects/.*$
RewriteRule ^(.*)$ /some/subdir/projects/$1 [L]
Wilco
  • 32,754
  • 49
  • 128
  • 160
7

Try this rule in your .htaccess configuration file in the document root of your server:

RewriteEngine on
RewriteRule !^some/subdir/projects(/|$) some/subdir/projects%{REQUEST_URI} [L]

As you want to use this rule in your /some/subdir/ directory, change the rule as follows:

RewriteRule !^projects(/|$) projects%{REQUEST_URI} [L]

And if you want to redirect any requests of /some/subdir/projects/foobar to /some/subdir/foobar, put this rule above the previous mentioned:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /some/subdir/projects[/?\s]
RewriteRule ^some/subdir/projects/?([^/].+)?$ /some/subdir/$1 [L,R=301]
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • How might I do this with an .htaccess file inside "/some/subdir" since I don't have write access to the server's web root? – Wilco May 14 '09 at 17:34
  • Remove the `some/subdir/` from both the pattern and substitute. The rest should be fine. – Gumbo May 14 '09 at 17:38
1

I would use this:

RewriteEngine On
RewriteRule ^(/some/subdir)/(.*)$ $1/projects/$2

This will redirect /some/subdir/<anything> to /some/subdir/projects/<anything>.

Note that the leading / is actually required to match the beginning of the URL, unless you have RewriteBase / set somewhere.

molf
  • 73,644
  • 13
  • 135
  • 118
  • The leading slash is only required when used in the server or virtual host configuration. In a .htaccess config file the per-directory prefix if removed before tested and later appended to the substition. – Gumbo May 14 '09 at 17:21
  • I was having massive headache with / missing from the subfolder, - not much info around on this problem, but if you just want to redirect to subfolder the person actually came from, do the following - RewriteRule /?([^/].+)?$ http://examplen.com/$1 [R=301,L] – GAV Nov 08 '17 at 13:05