Mod_rewrite and $_GET variables - php

If I am mod_rewriting a URL from:
http://www.mysite.com/blog/this-is-my-title/1/
to
http://www.mysite.com/blog.php?title=this-is-my-title&id=1
...is it possible then to arbitrarily attach a get value on to the URL later, or does the mod_rewrite throw it off?
MY REWRITE RULE:
RewriteRule ^blog/([A-Za-z]+)/(0-9]+)/? blog?title=$1&id=$2 [L]
EXAMPLE:
can i go
http://www.mysite.com/blog/this-is-my-title/1/?first=Johnnie&last=Wiggles
which would essentially mean
http://www.mysite.com/blog.php?title=this-is-my-title&id=1&first=Johnnie&last=Wiggles
I would think that should work, but for some reason it's not for me at the moment.

You can add QSA to the RewriteRule flags:
RewriteRule page_([0-9]+)\.html page.php?id=$1 [QSA]
Will redirect page_1.html?a=2 to page.php?id=1&a=2
However, be careful because requesting page_1.html?id=2 will redirect to page.php?id=1&id=2, and (in PHP), $_GET['id'] will be 2.

It's possible to append it, with the QSA (query string append) flag.
RewriteEngine on
RewriteRule {from-url} {to-url} [L,NC,QSA]

Related

PHP/Apache rewrite rules with multiple parameters?

I got the difficult when rewrite rules with multiple parameter,to modify a URL to SEO-friendly.
My URL:
http://domain/cat.php?alias=canon&sort=price&page=3
I want to have a rewrite rule so that the following:
http://domain/c/canon?sort=price&page=3
Heres my current rule:
RewriteEngine On
RewriteRule ^c/([a-z,0-9-]+)$ cat.php?alias=$1 [L]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)$ cat.php?alias=$1&sort=$2 [QSA]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)&page=([0-9]+)$ cat.php?alias=$1&sort=$2&page=$3 [QSA]
I try to get the params but it doesn't work. Anyone have any ideas on which rewrite rules to use?
Thank you!
--hatxi
RewriteRule ^c/([a-z,0-9-]+) cat.php?alias=$1 [L,QSA]
Should be enough. The QSA flag will take care of passing the sort and page parameters.
Your rules don't work because of the [L] flag on the first one, it just discards the rest because it always matches first.

.htaccess restricting $_GET

I have a project for modification, where I need to post a form with get, but I am unable to get the post value.
after checking I determine that .htaccess causing problem for this, please help me on this so I get the value without affecting the existing functionality of site.
this is .htaccess file
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)$ ?x=$1 [QSA,L]
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ? x=$1&y=$2&z=$3
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6&a=$7
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6&a=$7&b=$8
eg : http://www.exampledomain.com/result?term=sampleterm
in this the $_GET['x'] value is result, but I am unable to get term value
You actually need QSA flag (Query String Append), and its better to use L (Last) flag also.
RewriteRule ^/?([\w\s+-]+)/?$ ?x=$1 [QSA,L]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/?$ ?x=$1&y=$2 [L,QSA]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/([\w\s+-)/?$ ?x=$1&y=$2&z=$3 [L,QSA]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ ?x=$1&y=$2&z=$3&u=$4 [L,QSA]
# rest of the rules here...
QSA flag makes sure to append existing query string in the new query parameter you're adding through your rules.

extending a htaccess rewrite rule

I have following rewrite rule/condition in my htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^id([0-9]+)/?$ /profile.php?user_id=$1 [L]
The result is "example.com/id1"
Now the problem I have, I need to put a $_GET parameter sometimes to the URL, but actually they get ignored, example:
example.com/id1?section=video
example.com/id1?section=photo
example.com/id1?section=audio
Also not working:
example.com/id1&section=video
Is there a chance to accomplish this task by extending the htaccess rule? Remember that the URL not always have the $_GET parameter added.
Best regards!
Try using the QSA flag
RewriteRule ^id([0-9]+)/?$ /profile.php?user_id=$1 [L,NC,QSA]

.htaccess RewriteRule to preserve GET URL parameters

I'm having issues keeping the parameters of the URL working after an .htaccess URL rewrite.
My .htaccess rewrite is as follows:
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2
Which means:
example.com/index.php?lang=en&page=product displays as example.com/en/product
For some reason, when I add a ?model=AB123&color=something at the end of my URLs I am not able to retrieve those parameters in PHP using $_GET['model'] and $_GET['color'] even though they are present in the displayed URL.
Why aren't the variables passed along?
You need to append with the [QSA] (query string append) tag. Try
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2 [QSA]
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

mod_rewrite passing variables

I have the following mod_rewrite rule:
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1
This works fine in redirecting things like /blabla to /search.php?action=procedure&procedureName=blabla
The problem is that sometimes I want to pass a 'start' value (for pagination). For example, /blabla/?start=20.
Currently, it just ignores it. Printing out the $_REQUEST array doesn't show 'start'. I tried modifying the rule to:
RewriteRule ^([^/.]+)/\?start=([0-9]+)$ search.php?action=procedure&procedureName=$1&start=$2
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1
But it didn't do anything.
Any idea?
Thanks
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1 [L,NC,QSA]
The QSA means query string append, and it'll append $_GET vars you pass. Otherwise, they are normally not added.
RewriteRule applies to the path. You need to use RewriteCond to match the query string.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
Or to just pass the query string through, use %{QUERY_STRING}
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1&%{QUERY_STRING}
Actually I think I found my answer:
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1 [QSA]
The QSA allows it to pass query strings.
Right?

Categories