Just completing William’s reply.
If you need to redirect other Urls, you can do something like:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Remove qualquer possível / no final da URL:
RewriteRule ^(.*)/$ /$1 [L,QSA]
# Casa as URL /stream/foo para /pages/stream.php?a=foo:
RewriteRule ^stream\/(.*)$ /pages/stream.php?a=$1 [L,QSA]
# Casa as URL /foo para /pages/foo.php:
RewriteRule ^(.+)$ /pages/$1.php [L,QSA]
# Casa a URL / para /pages/index.php:
RewriteRule ^$ /pages/index.php
Thus, the following redirects occur:
site.com/ => site.com
site.com => /pages/index.php
site.com/foo/ => site.com/foo
site.com/foo => /pages/foo.php
site.com/foo/bar => /pages/foo/bar.php
site.com/stream/ => site.com/stream
site.com/stream => /pages/stream.php
site.com/stream/foo/ => site.com/stream/foo
site.com/stream/foo => /pages/stream.php?a=foo
This reply completes the other question user, which is practically the same as this, however, if modified the statement of this, William’s response may lose the meaning.
Question: If no path is passed in the URL, what happens? It would display the
index.php
root normally? Or once a rewrite rule has been defined it ceases to considerindex.php
, that is, if the URL does not match any set, the server tries to access the page normally?– Woss
@Andersoncarloswoss yes, so I used the
RewriteCond %{REQUEST_FILENAME} !-f
, in case no past path initially tries to access theindex.php
. Even if index.php does not exist in the folder it will not redirect in the above case because it is necessary 2 "Matches", something like:site.com/matchA/matchB
– Guilherme Nascimento
I get it. I’ve always felt that if I set a rule, I need to define it for every case, as I did in my answer with
^$
in case it was not informed the way. This makes things much easier xD– Woss
@Andersoncarloswoss is more in regex than in mod_rewrite, if the regex "home" everything and there is nothing defined in rewritecond or meets all their requirements, then passes and the
[L]
prevent othersrewriterules
, if there is no flag[L]
two rules can mix and mess, of course there is time that is desirable to mix.– Guilherme Nascimento