Mod rewrite with parameters - php

In fact, I am working on a small PHP script and I want to redirect this :
http://localhost/app-[NUMBERS].html?code=[LETTERS AND NUMBERS]
to this:
http://localhost/app.php?id=[NUMBERS]&code=[LETTERS AND NUMBERS]

RewriteRule ^app-([0-9]+)\.html /app.php?id=$1 [L,QSA]
This captures the numeric portion of the request, and converts it to the id query string parameter. The QSA flag deals with any existing parameters, and passes them through untouched.
e.g.
http://scratch.bagheera/app-12345.html?code=ABC123
->
http://localhost/app.php?id=12345&code=ABC123

Related

Can't get $_GET after Apache rewrite [duplicate]

I'm trying to rewrite an url from:
http://domain.com/aa/whatever/whatever.php
to
http://domain.com/whatever/whatever.php?language=aa
However, depending on existing $_GET variables, it either has to be ?language or &language.
To do this, I use 2 regexes with the [L] flag:
RewriteRule ^([a-z]{2})/(.*\.php\?.*) /$2&language=$1 [L]
RewriteRule ^([a-z]{2})/(.*) /$2?language=$1 [L]
The second one works as expected... The first one however is never hit (it falls through to the second regex, which does hit), even though Regex Coach does show me that it should.
edit:
If just read that I need to use two backslashes to escape the question mark. If I do this, it does hit on the first regex but never find the other GET variables.
From the documentation for mod_rewrite the pattern in RewriteRule matches against the part of the URL after the hostname and port, and before the query string so the query string is not included. That is why you don't get the other variables.
To add a new query string parameter language=xx whilst preserving any existing query string you need to use the QSA flag (query string append). With this flag, just one rule based on your second case should be sufficient:
RewriteRule ^([a-z]{2})/(.*) /$2?language=$1 [QSA]
You could setup the URL rewrite to pass the language to the php script via the PATH_INFO element of the $_SERVER superglobal. Just pass the language to the script like so:
foobar.php/en?args
In this case, $_SERVER[PATH_INFO] would equal /en

htaccess url rewriting, query parameters are not available in script

I am using following rule to rewrite a url in directory style,
RewriteRule ^post/([0-9]+)$ post.php?pid=$1
by using this i am directing localhost/post.php?pid=3 to localhost/post/3
but now I want to pass more parameters in default way like ?key=value.
for example localhost/post/3?comment_id=23
but this key value pair is not available in script.
When I do echo $_GET['comment_id'] it's not echo'ing anything.
How do I get it done.
You need to use QSA (query string append) flag. Change your rule to:
RewriteRule ^post/([0-9]+)$ post.php?pid=$1 [L,QSA]
QSA flag will append pid query parameter while preserving original query string in the URI thus you can do:
$_GET['comment_id']
and also:
$_GET['pid']

Passing $_GET variables to virtual directories via .htaccess

I have the following .htaccess Rewrite rule below which works for converting virtual directories to parameters, for example:
www.example.com/usa/ny/nyc gets interpreted by PHP as www.example.com/index.php?path=usa/ny/nyc.
What I can't seem to figure out is how I would change my regex below to handle parameters of the virtual directories themselves. For example, I want:
www.example.com/usa/ny/nyc/?display=off&settings=none to be seen by PHP as www.example.com/index.php?path=usa/ny/nyc&param=display:off,settings:none.
What makes it extra tricky is that the parameters won't always be those two options I used in the example above, they will change dynamically. Any ideas or suggestions of how to go about accomplishing this?
RewriteRule ^/?([a-zA-Z_\-/]+)$ index.php?path=$1 [L]
Assuming you want to pass the query string unmodified, you can use the [QSA] (query string append) option like so:
RewriteRule /(.+)$ /index.php?path=$1 [L,QSA]
You can find the documentation for the QSA option here. From the docs:
With the [QSA] flag, a request for /pages/123?one=two will be mapped
to /page.php?page=123&one=two. Without the [QSA] flag, that same
request will be mapped to /page.php?page=123 - that is, the existing
query string will be discarded.
So, your PHP script will see all the parameters as standard _$_GET parameters, rather than needing to do any other modification.
If you would prefer to treat the result more like a typical path element, you can use the following:
RewriteRule /(.+)$ /index.php/$1 [L,QSA]
In the above case, your query string will still be appended, however you will need to handle the path explicitly using $_SERVER['PATH_INFO'].

Detecting empty querystring with mod_rewrite

I have a strange problem, where to generate pages I am using my URL's as:
site/city/city-name/page-number
for that mod_rewrite is:
RewriteRule ^city/(.*)/(.*)$ tagcity.php?tag=$1&pnum=$2 [L]
But now the problem is if I delete the city-name and make it empty like "site/city//page-number" then it starts taking page-number as city-name.
Also can I have only one rewrite to serve pages with or without page numbers?
I know its a strange situation but unfortunatly some of my sites pages are indexed in Google as above.
How to detect that its empty so I can generate a 404 page?
Use a + instead:
RewriteRule ^city/(.+)/(.*)$ tagcity.php?tag=$1&pnum=$2 [L,QSA]
* means "0 or more repetitions", whereas + means "1 or more repetitions". Thus using a + requires that field to have at least something in it.
Also, you may want to make that [L,QSA] so that if there are any additional query parameters in the original URL, they'll be preserved. (QSA = Query String Append)
As requested in the comments, a rule with the ability to handle completely missing page number:
RewriteRule ^city/([^/]+)(?:/(.*))?$ tagcity.php?tag=$1&pnum=$2 [L,QSA]

Basic URL Rewrite Problem in PHP (mod_rewrite)

In my .htaccess, I have the following
RewriteEngine On
RewriteRule ^users/?$ users.php
RewriteRule ^users/([a-z]+)/?$ users.php?username=$1
Everything works as intended if I do
http://example.com/users/
http://example.com/users/joeschmoe/
and PHP will read "joeschmoe" as the value for
$_GET['username']
However, if I do
http://example.com/users/joeschmoe/?foo
PHP will not pick up
$_GET['foo']
Any idea why this is happening and how I can get it work? Thanks for your time!
Add [QSA] option to your RewriteRule, which will enable apache to append query string to redirected url:
RewriteRule ^users/?$ users.php [QSA]
RewriteRule ^users/([a-z]+)/?$ users.php?username=$1 [QSA]
You are probably looking for the "QSA" option to RewriteRule :
'qsappend|QSA' (query string append)
This flag forces the rewrite
engine to append a query string part
of the substitution string to the
existing string, instead of replacing
it. Use this when you want to add more
data to the query string via a rewrite
rule.
That page also states :
Modifying the Query String
By default, the query string is passed
through unchanged. You can,
however, create URLs in the
substitution string containing a query
string part. Simply use a question
mark inside the substitution string to
indicate that the following text
should be re-injected into the query
string. When you want to erase an
existing query string, end the
substitution string with just a
question mark. To combine new
and old query strings, use the [QSA]
flag.
In your case, something like this should do :
RewriteEngine On
RewriteRule ^users/?$ users.php
RewriteRule ^users/([a-z]+)/?$ users.php?username=$1 [QSA]

Categories