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]
Related
So I'm currently using my .htaccess to write my URL to be more URL friendly.
But I'm having trouble getting the fourth variable that could be more folders. I'll try and explain it as best I can below.
So at the moment I have the following URL working:
www.mysite.co.uk/first/second/third
Now for this I am using the rewrite rule:
RewriteRule ^e/([a-zA-Z0-9-/]*)/([a-zA-Z0-9-/]*)/([a-zA-Z0-9\-\/]*)$ index.php?var_1=$1&var_2=$2&var_3=$3 [L]
My problem comes when I one of the following happens:
www.mysite.co.uk/first/second/third/fourth
www.mysite.co.uk/first/second/third/fourth/fifth
www.mysite.co.uk/first/second/third/fourth/fifth/sixth
From the above I want the following variables to be returned:
$_GET['var_1'] = first
$_GET['var_2'] = second
$_GET['var_3'] = third/fourth
OR
$_GET['var_3'] = third/fourth/fifth
OR
$_GET['var_3'] = third/fourth/fifth/sixth
So basically I always have a 'var_1' and 'var_2' set but the third variable can be the rest of the URL.
I hope this makes sense and that it's possible.
I'm not sure why you your rule starts with ^e/
if your urls will be in format : www.mysite.co.uk/e/first/second/third/fourth
then use this regex:
RewriteRule ^e/([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
If you need it without /e/ at start www.mysite.co.uk/e/first/second/third/fourth
then remove /e/ from start
RewriteRule ^([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
Example above will work even with url-s like /first and /first/second but if you don't need it and you want to have this rule just in case of 3 or more params then you can simplify rule:
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-/]+)$ index.php?var_1=$1&var_2=$2&var_3=$3 [L]
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]
I'm losing URL parameters when I do mod_rewrite, and I don't understand why...
I've had to add a country code to the URL, for localisation. So my old URL:
The original URL:
www.domain.com/mail_confirmation.php?id=222
now looks like
www.domain.com/us/mail_confirmation.php?id=222
Mod rewrite should call:
www.domain.com/mail_confirmation.php?id=222?country=us
And this is the rule I need help with. It doesn't do what I expect it to, and loses the parameters along the way:
RewriteRule ^([a-zA-Z]{2})/(.+)\?(.+) $2?$3&country=$1
Another rule that might be affecting is this one, at the very beginning of the file:
RewriteRule ^([a-zA-Z]{2})/?$ index.php?pais=$1
Do you see any mistakes here? I'd appreciate your help!
Your RewriteRule needs to be
RewriteRule ^([a-zA-Z]{2})/(.+)$ $2?country=$1 [QSA,L]
Please, note that URL parameters are not available for matching within the RewriteRule. If you simply need to append an extra URL parameter you can do so along with the [QSA] flag which would take care of appending the original URL parameters for you.
I am currently coding a pagination script into many parts of my site, this has been a well needed and requested feature and I have finally been able to come round and start coding it, it is all going well, until I find that my rewritten urls don't like working with the pagination urls.
So, an example page on my site would be news.php. This file structure can be something like news.php?id=5. I have rewritten the url like so:
/news/5/
## Rewrite URL's for News & Dev ##
RewriteRule ^news/([0-9]+)/$ /news.php?id=$1 [L]
RewriteRule ^news/([0-9]+)$ /news.php?id=$1 [L]
RewriteRule ^news$ /news.php [L]
RewriteRule ^news/$ /news.php [L]
The pagination script I am using prepends two new variables in the url, the new url turns out to be this:
news.php?page=1&ipp=55id=5
I would really appreciate it if anyone could assist me in making the urls look better, as it defeats the object of having it in the first place if after they use the pagination, it changes the url back to a clunky and ugly url.
I don't want it to be required to have parts of the url, that is something I really don't want..
e.g I don't want the url to be required to be /news/1/55/5, instead id like it to be optional.
Thank you for your time, it is appreciated!
Additional Information
The links in my news script currently display like so:
news.php?page=1&ipp=55id=5
I don't like to see ugly urls like that, and want to make the url look better using mod_rewrite, It would be better if the urls would display like so:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
Also, to make it as user friendly as possible, I don't want any of the fields to be required as such, so for example I would like to have the following link accessible at all times without it requiring the other fields.
/news/ID/
Then, when the user clicks a pagination link, it would use the following link structure:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
This is all from user feedback of my site, and is something that people have been asking for. Problem is, I don't understand even simple .htaccess
Thanks
RewriteBase /
# add slash to end of url if not present (and do a redirect)
RewriteCond $0 !/$
RewriteRule ^news([^\.]*)$ $0/ [L,R=302]
# rewrite url with format /news/[<id>/[<page>/[<ipp>/]]]
RewriteRule ^news/(?:([0-9]+)/)?(?:([0-9]+)/)?(?:([0-9]+)/)?$ /news.php?id=$1&page=$2&ipp=$3 [L]
Not sure what ipp is supposed to be, but my guess is it shows the number of item per page. I would personally not like to have that in my url.
You can have :
news/id/page/ipp with
RewriteRule ^news(/?)$ news.php [L]
RewriteRule ^news/([0-9]+)-(.*)_([0-9]+)(/?)$ news.php?page=$1&ipp=$2&id=$3 [L]
news/1222-subjet-for-example_34
return :
news.php?page=1222&ipp=subject-for-example&id=34
use (/?) instead of create many rules ;)
Hope it's works for you.