Get full URL using htaccess - php

I want get the full url of a page request using htaccess
My code is as follows
RewriteEngine On
RewriteRule ^(.*)\.html$ session_page2.php?page=$1 [NC,L]
But in session_page2.php the $GET variable prints as follows
Array
(
[page] => rfiregtoday
)
where my actual url is http://localhost/xxxx/rfiregtoday.html#2009CalgaryWEBC
I want to get rfiregtoday.html#2009CalgaryWEBC any idea how to do this?

You can use $_SERVER['REQUEST_URI'] to get the original request path. This will not include the hash because that is never given to the server in an http request. I suggest you change it to a get parameter like mypage.html?hash=foobar.
If you want the GET parameters available in PHP after a redirect, use the QSA flag in your RewriteRule. This will add GET parameters to the rewrite, and make them available in PHP with $_GET.
More info about the QSA flag here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

Related

QUERY_STRING only displays first parameter

I can only access my 1st URL parameter. I think it is because I have ampersands both in my RewriteRule as well as my QUERY_STRING. How can I access each parameter correctly in my PHP script?
I am making a request to
http://example.com/subdir/api/transport/1/car?type=fast&color=blue"
In my .htaccess file I have the following line:
RewriteRule ^(.+)$ index.php?q=%{REQUEST_URI}&params=%{QUERY_STRING}&api=$1 [L]
In my PHP, displaying json_encode($_GET) produces:
{"q":"\/subdir\/api\/transport\/1\/car","params":"type=fast","color":"blue","api":"transport\/1\/car"}
You will notice that the "color":"blue" no longer has an equal sign and is not contained within "params".
If I therefore display `json_enocde($_GET['params']) all I get is the following:
"type=fast"
What can I do to acquire the full set of parameters? Is it an issue with my .htaccess or my PHP file?
You don't need to use %{QUERY_STRING} in target by using QSA flag that appends existing query string to target if you're modifying query parameters.
You can just use your rule as:
RewriteRule ^(.+)$ index.php?q=%{REQUEST_URI}&api=$1 [L,QSA]
Well, if you think about the transformation that takes place:
index.php?q=%{REQUEST_URI}&params=%{QUERY_STRING}&api=$1
becomes:
index.php?q=/subdir/api/transport/1/car&params=type=fast&color=blue&api=transport/1/car"
So, of course you are getting that for your $_GET.
What I would do is change the rewrite to:
index.php?q=%{REQUEST_URI}&%{QUERY_STRING}&api=$1
And then in your code you will get:
{
"q":"\/subdir\/api\/transport\/1\/car",
"type": "fast",
"color":"blue",
"api":"transport\/1\/car"
}
If you want the params, you can then just remove the q and api keys from your $_GET.

Hide parameter (query string) in url (htaccess)

How to hide parameters (query_string) in URL but send them to page hidden, not visible in address bar
example:
I have this:
http://localhost/project/company/redeem/category/Shirts/18?category_id=18
I want:
http://localhost/project/company/redeem/category/Shirts.html
But I want to get sent parameter in PHP $_GET['sent']
I tried to redirect to page with no parameters(no query_string) [R] and after that give page some parameters (silently) without [R] redirection (without changing address itself)
I tried to use RewriteRule in .htaccess with different flags and different ways but nothing worked
Please suggest.
As far as i am aware you can only hide the parameter key (in this case 'category_id') using the RewriteEngine:
RewriteEngine On
Then use a RewriteRule like this
RewriteRule ^project/company/redeem/category/Shirts/18/(regex of whatever you expect for your parameter)$ project/company/redeem/category/Shirts/18.php?category_id=$1
Not sure if the first 18 is coincidence or always the same as the category_id value. If it is you can also use
RewriteRule ^project/company/redeem/category/Shirts/(regex of whatever you expect for your parameter)$ project/company/redeem/category/Shirts.php?category_id=$1
If you want to use $_GET the parameters have to be written to the url somewhere. You can only try to hide it.
For example if you know each category by a name you could put those into the url instead of numbers. Lets say category_id 18 is "V-Neck Unisex-Shirts" you could use a url like
http://localhost/project/company/redeem/category/Shirts/V-Uni
and tell your script to use category "V-Uni" instead of "18".
.htaccess:
RewriteRule ^project/company/redeem/category/Shirts/([a-zA-Z-]*)$ project/company/redeem/category/Shirts.php?category=$1

How to get Dynamic URL With help of .htaccess and PHP for Good SEO

For example i have
3 php pages
www.example.com/page1.php?var1=data1&var2=data2&var3=data3
www.example.com/page2.php?var1=data1&var2=data2&var3=data3
www.example.com/page3.php?var1=data1&var2=data2&var3=data3
For good SEO . I need URL like
www.example.com/page1/data1-data2-data3
www.example.com/page2/data1-data2-data3
www.example.com/page3/data1-data2-data3
I got something URL rewriting with PHP but am confused how to implement it for multiple dynamic PHP pages.
i need all the variables for proper functioning of php pages
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(page1|page2|page3)/([^-]+)-([^-]+)-([^-/]+)/?$ /$1.php?var1=$1&var2=$2&var3=$3 [L,QSA,NE]
In your .htaccess you should add the line:
RewriteRule ^(\w+)/(\w+)-(\w+)-(\w+)$ /$1.php?var1=$2&var2=$3&var3=$4 [QSA]
The first part captures the page name (script name, in your case), then each successive query string parameter. The QSA key tells the redirect to take any additional query string parameters not specified explicitly along with the request.

PHP: Getting the url's value which is not parameter

As I know the only way to get a value from the url is to use the $_GET[]; . To do this the url must be of the format: http://domain.com/index.php?var1=value1
And then to do the following:
<?php
$value1 = $_GET['var1'];
echo($value1);
?>
What I really want is to get the value of the url in the format:
http://domain.com/value1
I know that is not a really php job, but while my index is index.php can I get the value1 from the url? Using something different rather than HTTP Get method.
Cheers.
[SOLUTION]:
I use the URL rewriting to make it work as #KevBot suggest. So what I did:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?value=$1 [L]
It's called URL rewriting. You need to modify .htaccess.
Here's a great tutorial:

Strip Youtube Link from URL

I own a website which allows users to download Youtube videos. I need php scripts which detects if a youtube url is present, like:
givemeyoutube.com takes users to my normal website but if they type,
www.givemeyoutube.com/watch?v=OZLUa8JUR18
how do I take them to the index of my website and strip out the "watch?v=OZLUa8JUR18" and store it as a variable?
UPDATE: How can I do it so that it checks URL and if it contains "watch?v=" then redirect to another page?
Use preg_match with the following regular expression
/(\/watch\?v=)([a-zA-Z0-9_-]+)/
Preg match with \b(?<=v.|/)[a-zA-Z0-9_-]{11,}\b as RegExp
You can to use mod_rewrite to properly take care of a request like watch?V=123456
in your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^watch index.php [QSA]
then in index.php:
if( isset( $_GET['v'] ) ) {
// access the v parameter with $_GET['v'];
}
I think url rewriting would be better suited for this task, as you need to rewrite the url anyway to make sure that watch opens an actual php page.
I would use something like:
RewriteEngine On
RewriteBase /
RewriteRule ^watch.*$ /index.php?action=watch [QSA]
This rule would cause all requests that start with watch to be routed to index.php with the original query string (due to [QSA]) and an additional parameter called action with the value watch.

Categories