# Htaccess issue

**URL:** https://forum.kirupa.com/t/htaccess-issue/262335
**Category:** Uncategorized
**Created:** [June 5, 2008, 6:02am UTC](https://forum.kirupa.com/t/htaccess-issue/262335 "2008-06-05T06:02:24Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![dColumbus](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dColumbus](https://forum.kirupa.com/u/dColumbus)
#### Post date: [June 5, 2008, 6:02am UTC](https://forum.kirupa.com/t/htaccess-issue/262335/1 "2008-06-05T06:02:24Z")

</div>

I’m trying to write some htaccess code that will prevent users from being able to go to a sub-directly directly or any directories within that sub-directory.

For example:

[http://www.website.com/sub-directory/](http://www.website.com/sub-directory/) - redirect to an error page  
[http://www.website.com/sub-directory/\*](http://www.website.com/sub-directory/*) (or anything else down this path) - redirect to an error page

Thank you in advance

---

<div class="post-metadata">

### Author: ![agnus](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@agnus](https://forum.kirupa.com/u/agnus)
#### Post date: [June 5, 2008, 8:01am UTC](https://forum.kirupa.com/t/htaccess-issue/262335/2 "2008-06-05T08:01:39Z")

</div>

Well, you will need something like this:

RewriteRule ^some\_directory/(.\*)$ error\_page.php [L,nc]

Let me know if this works.

---

<div class="post-metadata">

### Author: ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)
#### Post date: [June 5, 2008, 1:02pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/3 "2008-06-05T13:02:07Z")

</div>

You would have to add RewriteEngine on to make that work.

I would use this instead however;

```php

RedirectMatch permanent ^/some_directory/(.*)$ /error_page.php

```

---

<div class="post-metadata">

### Author: ![dColumbus](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dColumbus](https://forum.kirupa.com/u/dColumbus)
#### Post date: [June 5, 2008, 5:55pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/4 "2008-06-05T17:55:54Z")

</div>

[QUOTE=sekasi;2337210]You would have to add RewriteEngine on to make that work.

I would use this instead however;

```php

RedirectMatch permanent ^/some_directory/(.*)$ /error_page.php

```

[/QUOTE]

Interesting… I’m not so familiar with other htaccess rules like this. Would you recommend any particular website with clear and concise examples?

What’s so special or different about “RedirectMatch”?

---

<div class="post-metadata">

### Author: ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)
#### Post date: [June 5, 2008, 6:11pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/5 "2008-06-05T18:11:22Z")

</div>

RewriteRule and RedirectMatch belong to two seperate modules in your apache installation. There’s obviously a lot of things that differ, but the most important ones would be;

RedirectMatch is going to physically move you to the url you specify, with less control over the users address bar. The rewriteengine doesn’t need to be activated, hence less memory usage.

If you want the address bar to show something else than what you’re directing them to however, making a RewriteRule is better.

---

<div class="post-metadata">

### Author: ![dColumbus](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dColumbus](https://forum.kirupa.com/u/dColumbus)
#### Post date: [June 5, 2008, 6:18pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/6 "2008-06-05T18:18:40Z")

</div>

[QUOTE=sekasi;2337433]RewriteRule and RedirectMatch belong to two seperate modules in your apache installation. There’s obviously a lot of things that differ, but the most important ones would be;

RedirectMatch is going to physically move you to the url you specify, with less control over the users address bar. The rewriteengine doesn’t need to be activated, hence less memory usage.

If you want the address bar to show something else than what you’re directing them to however, making a RewriteRule is better.[/QUOTE]

Ah, alright. So, let’s say I needed to not only make sure that a user can’t access a directory, but then also rewrite the url, like so:

RedirectMatch permanent ^/some\_directory/(.\*)$ /error.php?error=2

then

rewrite “/error.php?error=2” to just “/error”

---

<div class="post-metadata">

### Author: ![dColumbus](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dColumbus](https://forum.kirupa.com/u/dColumbus)
#### Post date: [June 5, 2008, 6:54pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/7 "2008-06-05T18:54:45Z")

</div>

Alright,

I’ve got some issues. Although I do not want anyone to be able to access:

[www.website.com/admin-cp/\*](http://www.website.com/admin-cp/*)

I do, however, want users to be able to access:

[www.website.com/virtualfolder/admin-cp/\*](http://www.website.com/virtualfolder/admin-cp/*)

It can’t seem to figure out how to deny one, but allow the other… it keeps applying the rule no matter what.

---

<div class="post-metadata">

### Author: ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)
#### Post date: [June 5, 2008, 7:51pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/8 "2008-06-05T19:51:33Z")

</div>

try

```auto

RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-._/]*)/?
#rest of ur code here

```

---

<div class="post-metadata">

### Author: ![dColumbus](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dColumbus](https://forum.kirupa.com/u/dColumbus)
#### Post date: [June 5, 2008, 10:48pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/9 "2008-06-05T22:48:52Z")

</div>

[QUOTE=simplistik;2337508]try

```auto

RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-._/]*)/?
#rest of ur code here

```

[/QUOTE]

What exactly would the rest of the code be for my circumstance?

---

<div class="post-metadata">

### Author: ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)
#### Post date: [June 6, 2008, 2:55am UTC](https://forum.kirupa.com/t/htaccess-issue/262335/10 "2008-06-06T02:55:06Z")

</div>

[QUOTE=dColumbus;2337623]What exactly would the rest of the code be for my circumstance?[/QUOTE]

Whatever you figured out in the first 20 posts

---

<div class="post-metadata">

### Author: ![dColumbus](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@dColumbus](https://forum.kirupa.com/u/dColumbus)
#### Post date: [June 6, 2008, 5:55pm UTC](https://forum.kirupa.com/t/htaccess-issue/262335/11 "2008-06-06T17:55:05Z")

</div>

[QUOTE=simplistik;2337754]Whatever you figured out in the first 20 posts[/QUOTE]

Ok, not to be an \*\*_… but, I wouldn’t still be asking you questions if it was figured out. There are two ways of doing this, from what I understand… however, if you could explain what the heck "RewriteCond %{REQUEST\_URI} !^/([a-zA-Z0-9-.\_/]_)/?" even does… to me, all that looks like it’s doing it checking with a condition for ANY sub-domain name. I need mine to be specific.

This does not work:

RewriteCond %{REQUEST\_URI} !^/([a-zA-Z0-9-.\_/]_)/?  
RewriteRule ^some\_directory/(._)$ error\_page.php [L,nc]

Which, I believe is what you told me do to…
