How do i receive additional query parameters with htaccess - .htaccess

My url looks like this.
http://www.example.com/fb-ex1.php
http://www.example.com/fb-exg2.php
http://www.example.com/fb-exx3.php
Htaccess
RewriteRule ^fb-([a-zA-Z0-9-_]+)\.php$ fb.php?query1=$1 [L]
Now, When the user logs in, I guess i am not able to receive the code and state variable
How do i change the rewirte rule to receive additional query parameters?

Couple of mistakes:
hyphen should be at the start or at the end in a character class otherwise it needs to be escaped.
Not using QSA (query string append) flag. This flag ensures to preserve existing query parameters while adding new ones.
Your rule should be rewritten to:
RewriteRule ^fb-([\w-]+)\.php$ fb.php?query1=$1 [L,QSA,NC]

Related

.htaccess, virtual directories, and semi-complex URLs

I'm basically just trying to have a master syntax for predictable URLs. Simple URL is no problem
RewriteEngine on
# RewriteRule ^friendlyUrl/content/?$ /index.php?app=main&module=content
Which to my understanding looks for the url structure and allows 1 or 0 trailing "/"'s
But some parts of the website have a /urlPrefix/ to access, eg. mysite.com/membersArea/
and /membersArea/ will be apart of every query there. I'm having trouble accomodating for trailing ?s and &s in URLs like these.
RewriteRule ^secureUrl/\?(.*)$ /index.php?app=admin&$1
This is my attempt to handle everything from mysite.com/secureUrl/ to mysite.com/secureUrl/?var1=foo&var2=bar and after many server errors and a search, I find myself here.
This is the most complex line I have and between you and me, I couldn't tell you exactly what's happening other than it looks for /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar
RewriteRule ^friendlyUrl/([a-zA-Z0-9]{10})/?([a-z]*)/?\??(.*)$ /index.php?app=main&module=web&id=$1&$2&$3
Htaccess has always been my weakest subject, and as a webmaster I pay the price constantly, any help would be appreciated.
Need to input the same request to the PHP file (plus ANY query with or without ? or &) whether its just /friendlyUrl/ or /friendyUrl/?var=1, /friendlyUrl/&var=1, /friendlyUrl/var=1
You're looking to keep the query string of your request URI to remain as is, or to be included in the rewritten URL after the rewrite process is done.
For this purpose, you use the QSA flag in your RewriteRule directive. So, to rewrite /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar, you'd have:
RewriteRule ^friendlyUrl/([a-z\d]{10})/([^/]*)/?$ /index.php?app=main&module=web&id=$1&task=$2 [QSA]
Notice the QSA flag at the end. Also, keep in mind that I'm passing the second match (the possible task of your URL) as another variable (named task). This variable will be empty if nothing was found.
QSA|qsappend
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the
query strings to be combined.

Getting Query String item after htaccess redirect

I have a re-written URL that will look like one of the following:
http://www.example.com/anyfolder/some-more-here/
http://www.example.com/anyfolder/some-more-here/?ref=referralcode
Here is the rule I was playing with:
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ /page.asp?folder=$1&url=$2&refcode=$3 [NC,L]
My question is that since (http://www.example.com/anyfolder/some-more-here/?ref=) is a rewritten URL that has an additional query string item on it after the rewrite is done, how do I get that "ref" item in my code? I can easily get "anyfolder" and "some-more-here" by doing a simple request, but I am confused on the "ref" value part. I think the QSA flag might be needed, just having trouble bringing it all together.
I am using Classic ASP on this site, but the solution probably applies to PHP as well.
Any help would be great. thank you in advance!
Dennis
ok as per your comment you do need QSA flag. Try your rule as:
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ /page.asp?folder=$1&url=$2&refcode=$3 [NC,L,QSA]
This will preserve ?ref=... in the rewritten URI.
QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Rewrite rule in htaccess to keep parameters

I've written the following rewrite rule which works fine when no parameters (no page numbers, no products per page, no sort order etc)
RewriteRule ^(?!bench/).*cat_2.html(\.[a-z]{3,4})?(.*) "http\:\/\/www\.mysite\.co\.uk\/bench\/cat_2\.html\?mode\=allBrands" [R=301,L]
This makes sure the URL is optimized on Googles results. So
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands#
gets changed to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
mode=allBrands will always be set.
So if i click on a link to go to
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands&ppp=64&sort=desc&page=2
it gets redirected to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
which is the first page.
Any help would be great.
change [R=301,L] to [R=301,L,QSA]
More info : https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
You need to add a QSA flag to your rewrite rule, so that the brackets look like this:
[R=301,L,QSA]
This tells apache to append any existing query string to the new query string in the target (mode=allBrands).

rewriting an extra query string with mod_rewrite

I hope to learn how to do regular expressions one day but for now I need a little help.
I have this set up in my .htaccess. So that a query to example.doc is internally forwarded to download.php?file=example.doc
RewriteRule ^(download.php.*) - [L]
RewriteRule ^(.+\.(doc|docx))$ download.php?file=$1
I need it changed slightly so that an extra query sting (always present) on the document is also internally rewritten and passed to the php. For example: a query to example.doc?verify=whatever would be internally rewritten to download.php?file=example.doc&verify=whatever
Thanks!
Neddy
The [QSA] flag should take care of this (Query String Append).

htaccess redirect append query string?

I have an htaccess redirect that needs to forward the query string to the new URL, but it's getting dropped after the redirect. Can someone tell me what's wrong?
RewriteRule ^services/agents.*$ https://services.example.com/agents/ [R=301,L,QSA]
The same rule is working fine on my server. The problem should be something else.
I added the same rule on my server and I get the following redirect
http://mysite.com/services/agents/foo?foo=bar => https://services.mysite.com/agents/?foo=bar
Please note that you don't need to add the QSA flag since the target doesn't include any query string.
This article might contain some useful information to help you dealing with Htaccess and Query String.
In general there is no need to explicitly append the query or use the QSA flag if you don’t specify a query for the substitution. But as you said your rule doesn’t work, try this:
RewriteRule ^services/agents.*$ https://services.example.com/agents/?%{QUERY_STRING} [R=301,L]

Resources