How to use . htaccess with 2 arguments

Asked

Viewed 356 times

1

I looked on the internet, I found some examples, but I did not understand very well, for example, I need that when it is accessed site.com/stream/nome he runs the file stream.php, inside the archive stream.php I can get the value nome, how can I do this ??

2 answers

1

Would this be:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^stream/([^/]+)$ stream.php?nome=$1 [L,QSA]
  • The flag L prevents the next Rewriterules are executed if the match is accessed
  • The flag QSA passes the variables of querystring
  • The RewriteCond with !-f checks if file does not exist
  • The !-d checks if the folder exists
  • The $1 takes the value from within parentheses in ([^/]+)

Note that if stream.php is in a subfolder (such as shown here) must use:

RewriteRule ^stream/([^/]+)$ pages/stream.php?nome=$1 [L,QSA]

In case there is more than one PHP, according to the design:

inserir a descrição da imagem aqui

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-z0-9\-]+)/([^/]+)$ pages/($1).php?nome=$2 [L,QSA]
  • 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 consider index.php, that is, if the URL does not match any set, the server tries to access the page normally?

  • @Andersoncarloswoss yes, so I used the RewriteCond %{REQUEST_FILENAME} !-f, in case no past path initially tries to access the index.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

  • 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

  • @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 others rewriterules, if there is no flag [L] two rules can mix and mess, of course there is time that is desirable to mix.

0

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.

  • I used exactly the way you showed, just changing RewriteRule ^$ /pages/index.php for RewriteRule ^$ index.php why index.php is at the root, when I put any of these two RewriteRule ^stream\/(.*)$ /pages/stream.php?a=$1 [L,QSA] or RewriteRule ^(.*)/$ /$1 [L,QSA], any URL starts returning error 500.

  • Looking at the log returns the error Request exceeded the limit of 10 internal redirects due to probable configuration error.

  • In fact it’s not 100%. That’s the idea, but the execution isn’t exactly. I’m trying to find where I went wrong.

  • All right, anything I’m here, I’m doing tests too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.