Rewrite URL for root access but not block access to sub-domains

9

I have the following code that rewrites the URL you entered to correctly identify the areas, sub-areas, and content ID that the visitor is trying to access:

# Rewrite the url
<IfModule mod_rewrite.c>

  RewriteEngine On

  # Redirect when we have a single parameter
  RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?mod=$1
  RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?mod=$1

  # Redirect when we have two parameters
  RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?mod=$1&call=$2
  RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?mod=$1&call=$2

  # Redirect when we have three parameters
  RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?mod=$1&call=$2&id=$3
  RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?mod=$1&call=$2&id=$3

</IfModule>

Problem

In the domain there are sub domains that are no longer accessible if the .htaccess in the root contains the code above.

Structure

public_html        // root         http://www.meuSite.com/
public_html/cdn    // sub-domínio  http://cdn.meuSite.com/
public_html/app    // sub-domínio  http://app.meuSite.com/

Question

How to use URL rewriting for the parent folder, but still allow direct access to folders in the root (sub domains )?

    
asked by anonymous 25.01.2014 / 19:14

1 answer

7

RewriteCond

You should use the RewriteCond directive to add conditions for whether or not to apply redirection .

For example:

# Redirect when we have a single parameter
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?mod=$1

In this case, the !-f and !-d flags are determining that the redirect would only occur if there is no file or folder with the corresponding url .

Syntax

The syntax of the command is:

RewriteCond TestString CondPattern

Where CondPattern is a REGEXP compliant perl with some additions.

You can prefix the pattern with ! to invert its effect. There are some variations that allow you to use CondPattern without REGEXP too:

'<CondPattern'
'>CondPattern'
'=CondPattern'

The last three treat% literally% as literal, and then compare CondPattern with literal value of TestString

See some more conditions:

  • CondPattern Get the result of -d and check if it is an existing directory
  • TestString Get the result of -f and check if it is an existing file
  • TestString Same as -s , but only considers non-empty files
  • -f Checks whether the result of -l is path to a symbolic link
  • TestString Checks whether the result of -x is a path with permission + x
  • TestString Equals -F , but checks to see if the file is actually accessible by Apache. This implies making an extra internal request on the test, beware of overuse.
  • -f Same as -U , but tests by URL and not Path

All of these tests can be denied with a -F at the beginning.

Applying to the case study

A possible solution to address the question of the question:

RewriteCond %{HTTP_HOST} ^cdn\.meusite\.com\.br$ [NC]
RewriteRule ^([a-z0-9_-]*)(/([a-z0-9_-]*))?(/([0-9]*))?(/)?$
   cdn/index.php?mod=$1&call=$3&id=$5 [NC,L]

RewriteCond %{HTTP_HOST} ^app\.meusite\.com\.br$ [NC]
RewriteRule ^([a-z0-9_-]*)(/([a-z0-9_-]*))?(/([0-9]*))?(/)?$
   app/index.php?mod=$1&call=$3&id=$5 [NC,L]

RewriteRule ^([a-z0-9_-]*)(/([a-z0-9_-]*))?(/([0-9]*))?(/)?$
   index.php?mod=$1&call=$3&id=$5 [NC,L]
  

Do not break the rows in RewriteRule! I broke the example just for readability.

Note that this last case is more complex and we use some more advanced things:

  • We use the flag NC (nocase) to treat case equally
  • We use the flag L (last) to not process the rest of the rewrite if any of the conditions are satisfied
  • Instead of three different rows to meet one, two, or three parameters, we solve with one, transforming parameters two and three and the final slash into optional using ! in
25.01.2014 / 19:24