Htaccess regexp Underline and Space not working

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

I use this regex here and it works fine, but if I set it, for example:

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

This does not work. due to Space and underscore, I want to include spaces and underscore in regexp, but it doesn't work at all. Do I need to add something special?

And this also does not work:

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

I want to be able to enter something and open in the public_html folder.

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

This .htaccess expression is tested on HostGator servers using Apache 2.2.17 and I also tested Apache 2.2.17 in my localhost, and this also happens.

Error adding _ and space or. *:

Internal server error

The server detected an internal error or incorrect configuration and your request failed.

Contact the server administrator, admin @localhost and tell them the time when the error occurred, and anything you could do that could lead to an error.

Additional information about this error may be available if a server error logs in.

Thanks guys in advance.

+3
source share
1 answer

From regular-expressions.info :

The only special characters or metacharacters within the character class are the closing bracket ( ] ), backslash ( \ ), carriage ( ^ ), and hyphen ( - ). Regular metacharacters are normal characters within a character class and should not be escaped with a backslash.

You should not avoid characters in a character set other than those specified 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 can include this condition before the matching rule:

 RewriteCond %{ENV:REDIRECT_STATUS} ^$ 

Also, if you just want to combine everything as you said, I usually do something like below. Please note that %{QUERY_STRING} is passed to any GET vars. You can remove this if you do not plan to use regular GET-vars.

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

I hope this information helps you.

+3
source

Source: https://habr.com/ru/post/1492571/


All Articles