htaccess RewriteRule not processing if not all parameters are passed - .htaccess

Hoping someone can help me with this issue...
I have a RewriteRule set up in my htaccess file that looks like this
RewriteRule ^/?find/(.+)/?in/(.+)/(.+)/ /page.html?&type=$1&city=$2&state_abbrev=$3 [L,QSA]
The rewrite works fine if I have a URL that contains all the parameters specified for example: http://www.site.com/find/doctors/in/dallas/tx/
The issue is, I would like to have this rewrite work even if one of the parameters is missing. For example, if I only entered http://www.site.com/find/doctors/ I would still want it to redirect to 'page.html' and just not have the parameters completed. So in that case it would be writing to http://www.site.com/page.html?type=doctors&city=&state_abbrev=. Currently, if I type in a URL that doesn't have all parameters in it the RewriteRule will not work. Any ideas how to fix this issue?
Thanks in advance...

Replace your rule with this:
RewriteRule ^/?find/([^/]+)(?:/in/?([^/]*)/?([^/]*)/?)? /page.html?type=$1&city=$2&state_abbrev=$3 [L,QSA]

There is nothing stopping you from having multiple rewrites. I have many in my .htaccess for different things and many times you have to. Not one rewrite will work for all occasions.
You could try this.
RewriteEngine on
RewriteRule ^find/(.*)/in/(.*)/(.*) page.html?type=$1&city=$2&state_abbrev=$3&%{QUERY_STRING} [L]
RewriteRule ^find/(.*) page.html?type=$1&%{QUERY_STRING} [L]
It will first look for the full URL
http://www.site.com/find/doctors/in/dallas/tx
Then forward it to the first Rewrite rule.
But then if you used this
http://www.site.com/find/doctors
It will skip the first rule and it will match the 2nd rule redirecting to just the partial query string.
Hope that helps but you can make as many rules or conditions you want. And the order in which they come does matter.

Related

Opencart multistore "domain.com/aaa" instead of "aaa.domain.com"

Anyone can help to have "domain.com/store1/any-product" instead of "store1.domain.com/any-product"? The second is what I have and the first is what I would like to have.
Maybe some .htaccess code added? Anything else, please?
You should be able to achieve that by using the following rule in your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^store1.example.com
RewriteRule ^(.*)$ http://example.com/store1/$1 [L,NC,QSA]
What the above does is if the condition for store1.example.com is met, then it will rewrite the URL to example.com/store1. This will also work for any other directories you have as a part of it. So it would become example.com/store1/about for example.
Make sure you clear your cache before you test this.

mod_rewrite match from start of URI

Good afternoon,
I am having considerable difficulty getting my mod_rewrite rules to match from the start of the URI. I've looked through the manual but I must be missing some (probably obvious) syntax voodoo. Please consider the following:
RewriteCond %{REQUEST_URI} ^/(.*)/(foo|bar)/(.*)
RewriteRule ^(.*)/(.*)/(.*)/ destination.php?first=$1&second=$2&third=$3 [QSA,L]
If the 2nd URI parameter is foo or bar, (e.g. url.com/first/foo/third) this successfully redirects to destination, with the relevant parameters.
If foo or bar are present in the 3rd parameter (e.g. url.com/first/second/foo), I want a different redirect to occur. However, the following rule is ignored, with the above rule still taking priority
RewriteCond %{REQUEST_URI} ^/(.*)/(something-else)/(.*)
RewriteRule ^(.*)/(.*)/(.*)/ destination.php?first=$1&second=$2&third=$3 [QSA,L]
I think I must be missing an obvious way of forcing the rewrite to only match from the beginning of the URI- I've tried prepending slashes to try to force it to the root level, but without any joy so far.
Any help would be greatly appreciated!
Thanks
Edit
As it turns out, this was due to a basic RegEx mistake, rather than my mistakenly assuming the match needed to begin from the start of the URI. I won't edit the title as others may make a similar mistake.
.* is too greedy. Try these 2 rules:
RewriteRule ^([^/]+)/(foo|bar)/([^/]+)/?$ destination.php?first=$1&second=$2&third=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/(foo|bar)/?$ destination2.php?first=$1&second=$2&third=$3 [QSA,L]

htaccess rewrite all urls with a certain query to a parent directory

Sounds actually simple and at least solvable by researching. But unfortunately I don't get it done and need to ask for help.
I want to redirect all URLs which contain certain parameters to a parent directory. My solution only works if the url doesn't contain any additional parameter:
RewriteRule ^cars/mercedes-brands$ /mercedes? [R=301,NE,NC,L]
RewriteRule ^motorbikes/suzuki-brands$ /suzuki[R=301,NE,NC,L]
Redirect works for
http://www.example.com/cars/mercedes-brands ---> http://www.example.com/mercedes
http://www.example.com/motorbikes/suzuki-brands ---> http://www.example.com/suzuki
But not for
http://www.example.com/cars/mercedes-brands/test/123
http://www.example.com/motorbikes/suzuki/12?page=3
Any ideas? Thank you so much!
You need to specify there is (maybe) something after mercedes-brands and suzuki-brands.
Here are your 2 rules modified
RewriteRule ^cars/mercedes-brands(?:/.*)?$ /mercedes? [R=301,NE,NC,L]
RewriteRule ^motorbikes/suzuki-brands(?:/.*)?$ /suzuki? [R=301,NE,NC,L]

htaccess url masking mod rewrite

I have done some URL masking and it all works very nicely. I have one issue that I am trying to resolve now:
RewriteRule ^(.*)/(.*)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3
with the above I can access the same page 2 ways, www.domain.com/premier-league/2010-2011/arsenal/ and www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal
is there a way in my rewrite rule I can redirect the URL (301 ideally) is someone does it through the untidy way "www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal"?
Thanks in advance
If you add another rewrite rule after this one that matches your 'inverse' pattern, and mark both as the [L]ast rule, that might work. I first rewrite the url to include the query string, and [C]hain that one to the next rule. After that we [R]edirect the browser.
RewriteRule ^(.)/(.)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3 [L]
RewriteRule ^(.+) $1%{QUERY_STRING} [C]
RewriteRule ^teams.php?competition=([a-zA-Z0-9]+)&season=([a-zA-Z0-9]+)&teamid=([a-zA-Z0-9]+)$ $1/$2/clubs/$3/ [R=301,L]
Note I haven't tested this or the regex of the second rule. Might need to adjust the character ranges a bit. Also I haven't tested the query string rewrite in the second rule.
Edit: See here for some common use cases of mod_rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

.htaccess mod-rewrite question

hey there, i have quite some issues with the mode rewrite here is what i use :
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /creatii.php?creatie_thumb=$2&user=$1 [L]
RewriteRule ^([^/]*)/$ /creatiiuser.php?user=$1
i would like this link :
http://creatii.artcrew.ro/creatii.php?creatie_thumb=creatie19&user=dee-dee
to look:
like http://creatii.artcrew.ro/dee-dee/creatie19
well this is fine, it works, no problems with it but i want to make a rule for another link
http://creatii.artcrew.ro/categorii.php?numecat=poetry&numesubcat=satire
to look like
http://creatii.artcrew.ro/poetry/satire
how can i do this? what rules must i use?
currently if i access http://creatii.artcrew.ro/poetry/satire
it access the link : http://creatii.artcrew.ro/creatii.php?creatie_thumb=satire&user=poetry
how can i make both links(the first one and the second one) work?
one more thing, i want this link : http://creatii.artcrew.ro/creatiiuser.php?user=Dan to look like http://creatii.artcrew.ro/Dan or if that does not work http://creatii.artcrew.ro/user/Dan
how can i do that?
can anyone help me?
thanks in advance
Given the URL http://creatii.artcrew.ro/X/Y, how is mod_rewrite supposed to know whether X and Y are creatie_thumb and user values or numecat and numesubcat values?
You need to add something to the URL to differentiate these cases.
For example:
http://creatii.artcrew.ro/user/dee-dee
http://creatii.artcrew.ro/user/dee-dee/creatie19
http://creatii.artcrew.ro/cat/poetry/satire
RewriteEngine On
RewriteRule ^user/([^/]*)/([^/]*)$ /creatii.php?creatie_thumb=$2&user=$1 [L]
RewriteRule ^user/([^/]*)/$ /creatiiuser.php?user=$1 [L]
RewriteRule ^cat/([^/]*)/([^/]*)$ /creatii.php?numecat=$1&numesubcat=$2 [L]
Maybe you're too much generic with your rules and you got conflicts.
Try to map this urls
http://creatii.artcrew.ro/creatii/dee-dee/creatie1
http://creatii.artcrew.ro/categorii/poetry/satire
Starting from those sample you can easily map your urls with no conflict on rules
RewriteEngine On
RewriteRule ^creaati/([^/]*)/([^/]*)$ /creatii.php?creatie_thumb=$2&user=$1 [L]
RewriteRule ^categorii/([^/]*)/([^/]*)$ /categorii.php?numecat=$1&numesubcat=$2 [L]
Do some tries :D

Resources