Forum Moderators: phranque

Message Too Old, No Replies

.htaccess Two Redirects

         

Hornist

8:25 pm on Jun 15, 2015 (gmt 0)

10+ Year Member



I have a web site into which I am trying to put a public and private directory.

\.htaccess
\public
\private

The idea here is that all traffic gies to public except traffic with /dashboard in the url; which is directed to private.

So far I have:

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^(dashboard)($|/) - [L] <-----this is I think where the area of issue is...
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>

Any ideas would be greatly appreciated...

lucy24

11:28 pm on Jun 15, 2015 (gmt 0)

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



Is this htaccess file located in the directory that contains both /public/ and /private/ ? If so, the rule will always fail, because anchors in the pattern of a RewriteRule are determined by what physical directory it's located in. (Same as if you were in a <Directory> section in the config file.) That is, the ^ means "the current directory" not "the domain root". It's different from targets, which always start from the root unless you explicitly say otherwise.

#RewriteRule ^(dashboard)($|/) - [L]

What is this rule intended to do? As written, it doesn't do anything at all except stop execution of mod_rewrite. What's the capture for?

For that matter, what's the <IfModule> for? Either you've got mod_rewrite or you haven't. Note too, and far more seriously, that if you have RewriteRules in this location, you've pretty well given up the possibility of having further RewriteRules in the separate /public/ and /private/ directories where you really need them. That is, it's technically possible to have RewriteRules more than once along the same path, but things are very very unlikely to work the way you want.

You don't need ($|/) In fact you don't need anything after /dashboard/ unless you've also got some other directory whose name is, say, "dashboardwidgets". In that case you might have to throw in a \b ("word break").

Predominant question: Is this your own server? What does the DNS point to? Seems as if what you really need is not a RewriteRule at all, but something involving mod_alias that points the /dashboard/ requests to a different directory.

Hornist

12:23 am on Jun 16, 2015 (gmt 0)

10+ Year Member



The .htaccess fie is on the same level as the public and private directories. All are in the site root.

The following works well to direct all traffic to the public directory:

RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

What I am asking is what a rule would look like to intercept the traffic containing /dashboard/ and redirect it to the /private directory?

lucy24

12:53 am on Jun 16, 2015 (gmt 0)

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



It looks as if you simply didn't finish writing the intended rule,
RewriteRule ^(dashboard)($|/) - [L]

I can't do it for you because, among other things, only you know what you're rewriting to:
/private/dashboard/rest-of-path
or
/private/rest-of-path
or something else again. In any case, requests for example.com/dashboard without trailing slash will need an explicit redirect, which will have to be listed separately before the rewrite. (Search engines will ask for this form, and you can't leave it up to mod_dir because it isn't a real, physical directory.)

What happens if someone explicitly requests example.com/public/ or, for that matter, example.com/private/ ? I don't understand what prevents an infinite loop: example.com/public/public/public/public/ until the server gives up and throws a 500-class error.

Incidentally...
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
Why are there two rules? The form .* alone includes ^$ and there doesn't seem to be any special handling required, so it can all be expressed in a single rule if you've previously intercepted /dashboard/.

Oh, and do please put a / at the front of each target. It keeps malign robots from doing, er, whatever it is that malign robots do when given half a chance.

Edit:
redirect it to the /private directory

I'm assuming the word "redirect" here was simply a mistake.

Hornist

1:24 am on Jun 16, 2015 (gmt 0)

10+ Year Member



I am not an .htaccess expert. I pulled the rules for public from some examples in various forums. You; by way of contrast, are a blackbelt :) I will try this out, thank you very much!