How to use .htaccess with 2 arguments

1

I searched the internet, found some examples, but I did not understand it very well, for example, I need it when I access site.com/stream/nome it executes the file stream.php , but inside the file stream.php I can get the value nome , how can I do this ??

    
asked by anonymous 06.05.2017 / 00:54

2 answers

2

It would be this:

RewriteEngine On 

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

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

Note that if stream.php is in a subfolder (such as shown here ) you should use: / p>

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

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

RewriteEngine On 

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

RewriteRule ^([a-z0-9\-]+)/([^/]+)$ pages/($1).php?nome=$2 [L,QSA]
    
06.05.2017 / 05:35
0

Just completing Guilherme's answer.

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 answer completes the other user's question , which is pretty much the same as this, but , if you change the statement of this, Guilherme's answer may lose its meaning.

    
07.05.2017 / 17:18