I have the following code in my htaccess file:
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/$ index.php?page=$1&subPage=$2
It works great but I want to be able to take a url such as:
http://mysite.com/admin/somefolder/index.php?action=test
and have it bypass the rewrite rule and behave as it normally would.
I'm not too sure what the regex would be. Any help?
Thanks!
Use a Rewrite condition to limit the application of the rule.
I think this should work:
RewriteCond %{REQUEST_URI} !index.php
Haven't messed with mod rewrite in a long while, so make sure you test.
Related
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.
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.
I've got a .htaccess file that has got a rewrite rule in it as follows which works fine:
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]
What I'm looking to do is to keep using this for if the page variable is 2 or higher, but if it's 1 I want to 301 redirect to a separate url (the same site) say http://www.domain.com/solicitorsinCOUNTY/
The problem is that if I try doing this using a 301 redirect or a rewrite rule it still performs the above rewrite rule as well so I end up with http://www.domain.com/solicitorsinCOUNTY/?county=COUNTY&page=1
I haven't done much with .htaccess before so I'm not even sure if this is possible, can anyone help please? It would be much appreciated.
If you are using a rewrite rule, then put the rule for page=1 above the other rule and make sure you have the [L] flag.
Alternatively, you can use RewriteCond to prevent the rule from being run on specific URLS like this:
RewriteCond %{REQUEST_URI} !^solicitorsin([^/]+)/all/1$
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]
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
I'm not going to lie. I suck at writing htaccess rules. I'm ok with regexes though.
Ok, basically, user enters url: www.site.com/page.id or www.site.com/folder/page.id
That should be internally written to: www.site.com/index.php?page=id
I've got the stuff sorted in the index.php, but I just can't seem to hack it with the htaccess.
What do you suggest it should be?
EDIT: business/legal/service-level-agreement.10 and services.5 would need to work, so the rule would need to get the digit after the dot, period.
Try this rule:
RewriteRule ^([^/]+/)?([^/]+)\.(\d+)$ index.php?$2=$3
The simplest way would be:
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?page=$1 [L]
Take a look here maybe this can help:
generateit
cooletips
webmaster-toolkit