Hide parameter (query string) in url (htaccess) - php

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

Related

Htaccess 2 rewrite rules with post data

I've a link goes to a file with GET parameters.
Link in href="" is:
http://www.aaa.com/part1/part2/part3/
My htaccess is
RewriteRule ^(.*)/(.*)/(.*)/ detail.php?part1=$1&part2=$2&part3=$3 [L]
With this I get 3 data and show the URL as:
http://www.aaa.com/part1/part2/part3/
So far all works well. But i would like to change the URL but still get the GET values
The final result of URL should be:
http://www.aaa.com/part1-part2-text
I want to remove the / after part1 and part2 and remove part3 completly and put a text instead of part3.
I can rewrite the URL with:
RewriteRule ^(.*)/(.*)/(.*)/ part1=$1-part2=$2-text
but with this i'm not able to get the GET parameters.
Is there a way to combine these to rewrite parameters?
Thanks
You should read the mod_rewrite module first.
RewriteRule ^(.*)-(.*)-text$ detail.php?part1=$1&part2=$2

Change a url with parameters using htaccess

Like on the title, I'm looking for a way to change a url with parameters (from this www.animevid.net/player/?anime=d/dmc to this www.animevid.net/player/anime/d/dmc) using the .htaccess file.
I've found many similar post but I've only got errors, loop redirect, or...nothing. The nearest thing i've got is this code :
RewriteRule ^player/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) player/index.html?anime=$1&t=$2 [NC,L]
Also please note the "d/dmc" on "?anime=d/dmc", is a variable, another example is "c/codegeass", "s/sao" etc...
As mentioned above the final, rewritten url you give as an example is invalid. You should escape the last /:
https://www.animevid.net/player/anime/d%2Fdmc
and rewrite to:
https://www.animevid.net/player/?anime=d%2Fdmc
Then you have to change your regex strategy for this to work. Try something like that:
RewriteEngine On
RewriteRule ^player/([^/]+)/([^/]+) player/index.html?anime=$1&t=$2 [NC,L]

Dynamic URL alongside URL rewrite

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.

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.

$_GET issue after url rewriting in PHP

My original URL is customers.php?id=3 and I get customer ID by writing $_GET['id'].
Now I wrote rewrite rules in htdocs and my URL is customers/id/3. I'm trying to get customer ID by $_GET['id'] which is not working. Can anyone please tell me what should I do to get ID?
My URL Rewrite is
RewriteEngine On
RewriteRule ^customers/([^/]*)$ /customers.php?id=$1 [L]
Your rewrite rule is wrong; you are capturing everything after the customers/ section until the first forward slash and you are not allowing anything after that.
In you example the rule would not even trigger as your url has a forward slash and characters after it.
You should change:
RewriteRule ^customers/([^/]*)$ /customers.php?id=$1 [L]
to something like:
RewriteRule ^customers/id/(\d+).*$ /customers.php?id=$1 [L]
^^ Allow for extra stuff after the id so you could add the name of your customer or something like that
^^^^^ Require at least one digit and only digits
You could also get rid of the ID if you wanted to so that the number would come directly after customer/ but that is up to you.
If you use:
customers.php?id=3
Use:
$_GET['id']
not $_GET['customers'].
If you want to get "customers", use: customers.php?customers=3
Your rewrite will be like this:
RewriteRule ^customers/id/([0-9]+)$ customers.php?id=$1 [L]
There is a simple way to parse urls of that style (customer/id/123):
$aUrlPieces = explode('/', $_SERVER['REQUEST_URI']);
print_r($aUrlPieces);
The last 3 elements in the array will be "customer", "id" and "123".
You can go one step further and define action methods in your application that would take corresponding amount of parameters, that way your urls will be syntax checked by PHP.
In this case, action method "customer" would take 2 parameters $identifier_type and $identifier_value.
That way if you accidentally mistype "customers/" instead of "customer/" you will get "Method not defined" exception.
change your rewrite rule to this:
RewriteRule ^id/([^/]*)$ /customers.php?id=$1 [L]

Categories