I'm trying to rewrite a URL in PHP from example.com/homehub/whatweneed.php?sort=default to example.com/homehub/whatweneed/sort/default.
I've got the following code in my .htaccess file:
RewriteEngine On
RewriteRule ^sort/([A-Za-z0-9]+)/?$ ^whatweneed.php?sort=$1 [QSA,PT,L]
It's rewriting the URL to what I want but I can't seem to get any of the variables passed through via $_GET.
Try
$_SERVER['REQUEST_URI']
or
$_SERVER['REDIRECT_URL']
Related
I am trying to have a URL like the following:
http://example.com/product/category/name?ref=title
Where there is a URL Rewrite AND (at least 1) ?attribute=value at the end of the URL.
How do I get the value of that attribute with the '?' before it? I have tried just using the basic $_GET[''] to get the value, but it doesn't seem to work.
Here are a few ideas:
RewriteRule /page /index.php?attr=val [QSA]
You can also use %{QUERY_STRING} variable in the rewrite rule.
RewriteEngine On
RewriteRule ^about/([^/.]+)/?$ about.php?id=$1 [L,QSA]
I have a page use htaccess rewrite rule for url
/about/33
the page require $_GET['id'] to fetch db
however i have trouble to detect GET variable isset
if(isset($_GET['id'])){fetch data...}else{header("location:...");}
if user only enter /about/ without $GET['id'] the page will not found instead run header("location");
is anyway to solve this?
try this:
RewriteRule ^about/([^/.]*)/?$ about.php?id=$1 [L,QSA]
Put * instead of +
if i have a url like:
domain.com/profile.php?id=1&lang=en
Can i get a url like this using htaccess?
domain.com/1/en
thanks
This is how i would do it in .htaccess
RewriteEngine On
RewriteRule ^/1/en$ /profile.php?id=1&lang=en
I am trying configure my htaccess on my root directory such that if a person types:
www.example.com/link1/stuff
into their browser, 'stuff' will be stored as a variable later on to be used by a PHP file.
I figure that the best way to do it is to redirect it to another url and get the php using php $_GET.
www.example.com/link1/file.php?var="stuff"
Is there a better way of doing this?
Note:
I would like the link to stay exactly the same as
www.example.com/link1/stuff
even though it will be redirected.
use htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase www.example.com/
RewriteRule (.*)/(.*) www.example.com/$1/file.php?var=$2
OR
Options +FollowSymLinks
RewriteEngine On
RewriteBase www.example.com/
RewriteRule link1/(.*) www.example.com/link1/file.php?var=$1
you don't have to redirect to a different page and pass it as $_GET var. Simply use parse_url and access your needed fragment e.g. via:
$uri = 'www.example.com/link1/file.php?var="stuff'
$uri = parse_url($uri);
echo $uri['path'] //returns /file
http://de3.php.net/manual/en/function.parse-url.php
In a website I found, they use friendly URLs like this:
Real URL:
example.com/index.php?mode=product
FRIENDLY URL
example.com/view/product.html
In the friendly URL, there is a feature to get a variable with $_GET function. So if the URL looks like this:
FRIENDLY URL 2
example.com/view/product.html?id=10&lang=en&cur=1
This is similar to the friendly URL but allows me to easily access variable parameters.
Can someone help me write an .htaccess rewrite rule like this?
The [QSA] directive in mod_rewrite is your friend here. It will append all the other query string parameters onto the end of the rewritten url:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^view/(.*).html /index.php?mode=$1 [QSA]
That is untested, but just a quick answer that should get you going.