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.
Related
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]
I'm having e commerce website in php. And now moving to fancy urls, url rewriting.
for that i've variable length of parameters with optional variables too.
for example,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
Here, the parameters are optional That means, some variables may or may not participage in the url except id.
here, page=e is fixed and it is not dynamic. ( the reason behind is i've other rewritten rules like ^categories, ^toys etc..
and re-written url should be one of these,
http://example.com/e/23/color-blue/size-xxl/
http://example.com/e/26/color-black/width-500/height-900/
http://example.com/e/56/type-shirt/color-white/height-345/size-xl/
i've tried below with no luck,
RewriteRule ^e/([^/.]+)/?$ product.php?page=e&url=$1 [L,NC,QSA]
i'm getting all $_GET values like this,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
but i'm trying to achieve like this,
http://example.com/e/23/color-blue/size-xxl/width-200/height-100
how can i pass the queries with slashes and not &'s.
that main idea behind above last url is to process the whole fancy url into simple one in php,
and then use those variables in the page script. is there any better solution than this ??
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(e)/([0-9]+)/([^-]+)-([^/]*)(/.*)?$ product.php$5?page=$1&id=$2&$3=$4 [L,QSA]
RewriteRule ^(product\.php)/([^-]+)-([^/]*)(/.*)?$ $1$4?$2=$3 [L,QSA]
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.
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]
This is my URL rewrite:
RewriteRule ^cars/$ cars.php
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1
This works, so my URL is like this:
example.com/cars/porche/
refers to
cars.php?model=porche
Now Im making more search criterias, so I want to be able to add for example model year, car manufactor, etc. like this:
example.com/cars/porche/?model_year=xxx&car_manufactor=xxx
Right now this does not work with the current rewrite, but I can't figure out why.
Simple, just add QSA:
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1 [QSA]
That tells the rewrite engine to append any other query parameters to the rewritten URL as well.