3
RewriteRule ^([A-Za-z0-9'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1 [NC]

I'm using this Regex up here and it works just fine, but if I put for example:

RewriteRule ^([A-Za-z0-9 _'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ\/\.\-]*)$ public_html/$1 [NC]

This up here doesn't work. because of the Space and underline, I want to include spaces and underline in the regexp, but it doesn't work at all. do I have to add something special to it ?

And this also doesn't work:

RewriteRule ^(.*)$ public_html/$1 [NC]

I want to be able to type anything and open in the public_html folder.

Ex, I type: www.mysite.com/site_1.php then it opens: www.mysite.com/public_html/site_1.php

This .htaccess expression is being tested in the HostGator servers using Apache 2.2.17 and I also tested in my localhost Apache 2.2.17 as well, and same happens.

The Error given when adding the _ and space or .* is this:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Thank you guys in advance.

Monolo
  • 18,205
  • 17
  • 69
  • 103
Grego
  • 2,220
  • 9
  • 43
  • 64
  • 2
    Escape the space, otherwise Apache will treat it as the end of the regex pattern. so make it `\space` instead of just `space`. As for the internal server error, look at the server's error logs. It'll have exact details of what caused it (probably the space char making the rewriterule have syntax errors). – Marc B Oct 12 '11 at 18:06
  • Thanks for answering, that helped in the space problem, but what about the rest? the .* and the underline? – Grego Oct 12 '11 at 18:11
  • Don't think `_` is a metachar in those regexes. But try escaping it anyways and see what happens. You still get an internal error when using `_` or `.*`? Check the error log again to see if it's a different message. – Marc B Oct 12 '11 at 18:12
  • I tried to escape, but its not a metachar, I delete the log files and just loaded the page, and no errors appear there. Maybe there is another way to accomplish matching all characters? – Grego Oct 12 '11 at 18:21
  • 2
    ^(.*)$ is going to match everything every time, so you'll end up in a continuous loop. You'll need a RewriteCond to make sure you're not already in public_html. You should be able to add `_` without a problem, you to handle the space try using `\s` - I believe that's the metachar for spaces in regex. – Dan Ambrisco Oct 13 '11 at 05:22
  • Just add `RewriteRule ^public_html/ - [L]` in front of the other rule to prevent a continuous loop of redirect, like dambrisco described. – Gerben Dec 02 '11 at 16:36
  • I suggest using RewriteCond %{ENV:REDIRECT_STATUS} ^$ to avoid the continuous loop. This is actually checking whether or not it was just redirected. Also, you should be fine with a space in your regex since it's within the character class. You also have begin/end anchors (^ and &), so the parser should definitely not think the space is the end of the expression as someone else suggested. – rocksfrow Dec 27 '11 at 06:42
  • I also think it's important to add that the \s shorthand is NOT the same as a literal space. \s will match a space, a tab or a line break. So keep that in mind if you decide to use \s instead. Another one you could consider using is \w, which matches basically the first half of your regex ([A-Za-z0-9_]). Another negative to consider is when using shorthand characters you're decreasing the flexibility of your regex, and also running risk of the regex meaning two different things on different systems parsing it. I typically like to specifically specify what I want to match to be sure. – rocksfrow Dec 27 '11 at 06:52

1 Answers1

3

From regular-expressions.info:

The only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash.

You shouldn't be escaping characters within the character set other than the ones mentioned above. The simple trick here is to make sure you keep the hyphen at the VERY END of your character class. This identifies the hyphen as a literal.

RewriteRule ^([a-zA-Z0-9'"%ãõáéíóúâêîôûàÁÃÕÁÉÍÓÚÂÊÎÔÛÀ _/.-]*)$ public_html/$1 [NC]

To avoid a "continuous loop" you could include this condition before your match rule:

RewriteCond %{ENV:REDIRECT_STATUS} ^$

Also, if you simply want to match everything like you also talked about, I usually do something like below. Note, the %{QUERY_STRING} is there to pass on any GET vars. You can remove that if you don't plan to use regular GET vars.

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?request=$1&%{QUERY_STRING}

I hope this information helps you out.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
rocksfrow
  • 851
  • 8
  • 12