Skip to main content

mod rewrite - checking query string in HTACCESS file



Using mod rewrite rule in htaccess

Using Query string check in htaccess


Question:
Say I want to forward this URL: /cansas.php?m=2&id=2-0-0-0&sid=cansas to /cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].

I have this in my .htaccess:

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]

I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.

How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:

/cansas.php?m=2&id=2-0-0-0&sid=cansas

/cansas.php?m=2&id=2-1-0-0&sid=cansas

/cansas.php?m=2&id=2-2-0-0&sid=cansas

Any help would be greatly appreciated :)

Maybe it isn't possible to do it this way? The way I have set it up at the moment is that I want to use new URLs called /cansas-is-good-for-you which reads from the source /cansas.php?m=2&id=2-0-0-0&sid=cansas, but the URL shown in the browser should be: /cansas-is-good-for-you. I need to forward that old cansas.php? URL to the new URL :)


SOLUTION : Scroll down




You need to check the query of a URL with the RewriteCond directive as the RewriteRule directive only handles the URL path:

RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

If you want to check for just one parameter, use this:

RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

And to do this just for initial requests, you need to check the request line:

RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]





mod_rewrite Flags

* last|L - The Last flag tells Apache to terminate a series of rewrite conditions and rewrite rules in the same way that } will terminate a statement block in PHP. Note that this flag does not terminate mod_rewrite processing!


* nocase|NC - The No Case flag tells Apache to ignore the case of the string in the regex and is often necessary when writing a RewriteCond statement that matches the {HTTP_HOST} variable for a domain name, which is not case sensitive.


* redirect|R - The Redirect flag is used to trigger an external (visible) redirection. By default, this means that Apache will issue an HTTP 302 response to indicate that the document has been moved temporarily, but you can specify the HTTP code if you like. For example, you could use [R=301] to make Apache issue a HTTP 301 response (moved permanently), which is often useful if you need search engines to reindex a changed URI.


* qsappend|QSA - The Query String Appended flag is used to "pass through" existing query strings. You can also define a new query sting to which the old string will be appended, just be careful not to replicate key names. Failure to use the QSA flag will cause the creation of a query string during a redirection to destroy an existing query string.


* forbidden|F - The Forbidden flag is used to tell Apache when not to provide a page in response to a request. As a result, Apache will issue a HTTP 403 response, which can be used to protect files from being viewed by unauthorized visitors, bandwidth leeches, and so on.


* ornext|OR - The OR flag allows you to combine rewrite conditions with a logical OR relationship as opposed to the default AND.


* next|N - The Next flag tells mod_rewrite to restart the rewriting process from the first rule, but to use the URI that was returned from the last processed rewrite rule. This is useful for replacing characters in the {REQUEST_URI} when you don't know how many there will be.

Reference
Site Point
stackoverflow.com
Apache tutorial

Comments

Popular posts from this blog

Strange problem occured while trying to create a CSV file using PHP Script - The file is not seen on FTP but can download using file's absolute path url

Strange problem occured while trying to create a CSV file - The file is not seen on FTP but can download using file's absolute path url Last day I came across a strange problem when I tried to create a csv file on therver using a PHP script. the script was simply writing a given content as a csv file. The file will be created runtime. What happened was, The script executed fine, file handler for new file was created and contents was wrote into the file using fwrite and it returned the number of bytes that was written.

How to get the Query string of a URL using the Javascript (JS)?

JS function get the Query string of a URL or value of each parameter using the Javascript(JS)? If you want to get your current page's url var my_url=document.location; to get the query string part of the url use like this: var my_qry_str= location.search; this will return the part of the url starting from "?" following by query string Lets assume that your current page url is http://www.crozoom.com/2013/page.html?qry1=A&qry2=B then the location.search function will return " ?qry1=A&qry2=B " to exclue "?", do like this:


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog