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.
Related
I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.
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]
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]
I would like to use mod_rewrite to append a parameter to the end of a querystring. I understand that I can do this using the [QSA] flag.
However, I would like the parameter appended ONLY if it does not already exist in the querystring. So, if the querystring was:
http://www.mysite.com/script.php?colour=red&size=large
I would like the above URL to be re-directed to
http://www.mysite.com/script.php?colour=red&size=large&weight=heavy
Where weight=heavy is appended to the end of the querystring only if this specific parameter was not there in the first place! If the specific parameter is already in the URL then no redirect is required.
Can anybody please suggest code to put in my .htacess file that can do this?
EDIT: tested and modified it, now it works for me
I assume you want to add a default weight parameter if there is no weight at all:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} !weight=
RewriteRule ^script.php script.php?weight=heavy [R,L,QSA]
This is esentially the same what #coreyward answered, but a bit more specific. (R flag, to make the change visible, and the weight parameter does not have to be heavy.)
Hope this helps!
This is a really strange thing to do. It's as though you're setting a default for your system, which is a pretty standard thing to do. The problem is that it's unmaintainable and awkward to be setting that default in your VHost definition or .htaccess file where you're literally configuring Apache.
I advise against doing it this way, but this is a quick and dirty way to do what you're looking for:
RewriteCond %{QUERY_STRING} !weight=heavy
RewriteRule /script.php /script.php?weight=heavy [QSA]