How to add additional URL variables to a URL already rewritten with mod_rewrite - .htaccess

Does anyone have any idea if it is possible to put additional variables into a URL that you are using mod_rewrite to basically chop up into variables.
For example, if I have the URL:
website.com/post/53/post-title/
and I am using mod_rewrite to turn it into:
website.com/post.php?postid=53
Is there and elegant way to put additional variables into the "pre-rewritten" URL and keep them for my post.php script?
I.E., what if I create a link like the following:
website.com/post/53/post-title/?anothervar=6
So far it seems like my mod_rewrite code is just throwing out the additional variable and sending the URL to the post.php script like this:
website.com/post.php?postid=53
I do know that I can use $_SERVER['REQUEST_URI'] to get the original URL in my php script (AKA website.com/post/53/post-title/?anothervar=6) before it gets rewritten by mod_rewrite and then just chop up the string to get that added on variable, but I just wanted to know if there was a more elegant solution that solely used mod_rewrite.

you can also use %{QUERY_STRING} to pass original query string to new url. or use it as you wish e.g.
RewriteRule /pages/(.*) /page.php?page=$1&%{QUERY_STRING} [R]

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
"qsappend|QSA"
Use, QSA to make mod_rewrite properly "merge" additional arguments when rewriting.
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
That will add "page=$1" to whatever request the user made.
PS. Although take care, because mod_rewrite will happily overwrite "page" though if the user specifies it. No way around it that I know off.

Related

RewriteRule from download.php?token=something into /download/something

I am working on url, I try htaccess, php, javascript and many other things but unable to figure it out.
My url is :
example/cheap-flight-to.php?country=lagos
and I want to change the url something like this :
example/cheap-flight-to.lagos
or
example/cheap-flight-to/lagos
please help me
The following should allow you to generate your urls in the format that you wish.
RewriteEngine On
RewriteBase /
RewriteRule ^example/cheap-flight-to/([a-zA-Z]+)$ /example/cheap-flight-to.php?country=$1 [NC,L]
What you want could be done using regular expressions in .htaccess, but it makes no sence, since it wouldn't be pointing to anything, unless you have a directory cheap-flight-to/lago in which you have an index.php that will show-up in the browser or return data. This means you have to setup a directory for each destination you want to go to. Is that really what you want? Usually it's been used the otherway around. The user (or a script for that matter) enters a url like "example/cheap-flight-to/lagos". Then through regular expressions in .htaccess you rewrite the url to "example/cheap-flight-to.php?country=lagos".
Have a look at http://httpd.apache.org/docs/1.3/misc/rewriteguide.html and
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for more on rewriting rules in .htaccess.

.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.

Htaccess parameter overwrite + allow additional params?

I have a rule in my .htaccess file to overwrite ugly parameter urls with clean paths.
Here is an example:
RewriteRule ^landing(/)?([0-9]+)?(/)?$ landing/index.php?intPage=$2
So when someone goes to /landing/3, it would load the index.php page passing intPage parameters as 3.
Very simple.
However, I'm using techniques to trace Google Analytics referals, so I need to tack parameters onto this URL:
For example:
/landing/3?kt_type=ad&kt_st1=adwords&kt_st2=prize_a_us.en
The problem is that, I don't know how to retrieve the parameters that are tacked onto the URL, since the URL has already been overwritten and now I can't retrieve kt_type, kt_st1 etc.
Any idea on how to make it possible so I can still tack parameters to already overwritten URLs?
Use QSA flag. Change your rule to this:
RewriteRule ^landing/?([0-9]+)?/?$ landing/index.php?intPage=$1 [L,NC,QSA]
From official docs:
QSA - Appends any query string from the original request URL to any
query string created in the rewrite target

URL Rewriting based on form input

I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:
site.com/go/?id=whateverIType
and I want to change it to
site.com/go/whateverIType
I was thinking on Mod Rewrite, but if the user put something in the URL, like:
site.com/go/?dontwant=this&id=whateverIType&somemore=trash
I want to ignore the other variables but "id", and rewrite the rule.
What's the better way of get this done? Thanks in advance!
PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".
I'm not familiar with CodeIgniter, but you can try the following RewriteRule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.
Hope this helps.
Mod_rewrite supports conditions and rules with RegEx, so you could have a rule that matched the ?id=XXXX, that would extract it from the URL (keeping the other parameters), and rewrote the URL accordingly.
However... I don't think you want to do this, because if you rewrite the URL to be /go/Some+Search+Query, you won't be able to pick it up with say, PHP, without parsing the URL out manually.
It's really tough to have custom, SEO-friendly URLs with user input, but it is technically possible. You're better off leaving in the ?id=XXX part, and instead, using mod_rewrite in the opposite approach... take all URLs that match the pattern /go/My+Search+Terms and translate that back into something like ?id=My+Search+Terms, that way you'll be able to easily parse out the value using the URL's GET parameters. This isn't an uncommon practice - Google actually still uses URL parameters for user input (example URL: http://www.google.com/search?q=test).
Just keep in mind that mod_rewrite rewrites the URL before anything else (even PHP), so anything you do to the URL you need to handle. Think of mod_rewrite as a regular expression-based, global "Find and Replace" for URLs, every time a page is called on the server. For example, if you remove the query string, you need to make sure your website/application/whatever accounts for that.
In application/config/routes.php
$route['go/(:any)'] = "go/index/$1";
Where go is your controller and index is the index action.
http://codeigniter.com/user_guide/general/routing.html
You can use something like this in your .htaccess if you aren't already:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Get htaccess to add value as GET variable when redirecting

Simply put, what I want to achieve is this,
www.example.com/show-products/food should be redirected to
www.example.com/show_products.php?cat_id=1
or www.example.com/show-products/clothes should be redirected to
www.example.com/show_products.php?cat_id=8
But, I want htaccess to send the GET variables from the URL to the script as well.
So www.example.com/show-products/food?sort=price&sort_order=asc should be redirected to
www.example.com/show_products.php?cat_id=1?sort=price&sort_order=asc&cat_id=1
I know the way I formulated the question is not the best but hopefully you can make sense of it.
Thanks.
I found the answer, it's easier than I expected, the condition is:
RewriteRule ^food(/)?$ show_products.php?cat_id=1&%{QUERY_STRING}
The way that you've done it is fine, but keep in mind that you can also use the QSA flag. This automatically appends the existing query string to the query string which you generate with your rewrite, handling the concatenation for you automatically.
This would look like the following:
RewriteRule ^food(/)?$ show_products.php?cat_id=1 [QSA]

Resources