.htaccess redirect with exception - .htaccess

In one of my projects I'm trying to redirect using .htaccess. I am using a redirect like this:
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
But, now I'd like to add an exception to the rewrite rules. I want http://example.com/admin/ to be redirected in the same way, only to a different directory. All of the other traffic should still go to the public directory. So i added a rewrite condition like this:
RewriteCond %{REQUEST_URI} !/admin/
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
This didn't do the trick. I am still being redirected to the public folder. I also tried this:
RewriteRule ^admin$ admin/ [L]
RewriteRule admin(.*) admin/$1 [L]
RewriteCond %{REQUEST_URI} !/admin
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
This only made things worse. I now get an 500 internal server error.
I have been searching for hours now, and I'm getting a bit tired of finding examples that show how to add an exception for a certain folder, but don't show how to redirect this traffic to another folder. Can you tell me what I'm doing wrong, or which other approach I could try?

A RewriteCond only applies to the rule directly beneath it. In other words, in your example, it's only applied to the empty path rule. Move the RewriteCond down one notch, and it will work better.
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !/admin/
RewriteRule (.*) public/$1 [L]
Additionally, I find your rule
RewriteRule admin(.*) admin/$1 [L]
to be odd. A URL like /adminsomething will be rewritten to /admin/something which I don't think is what you want.
Furthermore I have a hunch that you're confusing a rewrite with a redirect. A rewrite is a rule that makes a request be rewritten internally and interpreted differently. A redirect is a rule that will tell the visitor's browser to go to a different address. Which one are you going for?
The magic keyword for a redirect is R=301 which means redirect with http code 301 Moved Permanently. For example, you may want to try...
# This first rule is actually redundant in this case so skip it...
#RewriteRule ^$ public/ [R=301,L]
RewriteCond %{REQUEST_URI} !/admin
RewriteRule (.*) public/$1 [R=301,L]

Related

.htaccess mod_rewrite redirection not working

I am using the following condition/rule on a .htaccess file located at the root of my domain. The purpose is to redirect all non-www requests to their www. version:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
It seems to be working fine.
Inside my /blog/ subdirectory I have another .htaccess that I use to redirect fancy URLs to real ones:
RewriteCond %{THE_REQUEST} !.*?url=.*
RewriteRule (.*) index.php?url=$1 [QSA]
This also seems to be working fine. However, all non-www requests inside the /blog/ subdirectory are not being redirected to their www. version.
For example, if I type domain.com on the browser I correctly get redirected to www.domain.com. But if I type domain.com/blog/ or domain.com/blog/test-page/ I won't get redirected to the www. version.
Probably the .htaccess inside /blog/ is conflicting with the one at root level, but I don't know how or how to fix it. Any clues?
Update: I solved the problem by putting all the rules on the root .htaccess file. I had to tweak the fancy URL rules slightly to only catch the /blog/... requests. Here's the final .htaccess file in case it might help you:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^apprush\.com$ [NC]
RewriteRule ^(.*)$ http://www.apprush.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/blog
RewriteCond %{REQUEST_URI} !.*?url=.*
RewriteRule blog/(.*) blog/index.php?url=$1 [QSA,L]
Are you able to put all of the rewrite rules into the root level .htaccess? It not only gets around the problem you're having but is a little neater because you know exactly where all of your rules are located.
If you are, these rules will do what you need:
RewriteCond ${HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond ${THE_REQUEST} !/blog/.*?url=.*
RewriteRule (.*) index.php?url=$1
This is because you have dollar sign $ at the end of domain.com. Also you need to use REQUEST_URI instead of HTTP_HOST. Remove it:
RewriteCond %{REQUEST_URI} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
The dollar sign signifies the end of the string, thus making the rewrite to work for direct requests to domain.com. The update script solves the problem.

.htaccess seems to continue after Last flag

In the small portfolio website I am making I want to have clean urls. For this I use a .htaccess file. At the moment my website runs at 127.0.0.1/website. My .htaccess looks like this:
RewriteEngine On
RewriteBase /website
RewriteRule ^about$ index.php?page=0 [L,NC]
RewriteRule ^portfolio$ index.php?page=1 [L,NC]
RewriteRule ^cv$ index.php?page=2 [L,NC]
RewriteRule ^contact$ index.php?page=3 [L,NC]
RewriteRule ^.*$ about [L,R]
If I remove the last line, everything works fine. I can go to 127.0.0.1/website/about, and the server shows me 127.0.0.1/website/index.php?page=0, while the url does not change. However, I want every other url to be redirected to 127.0.0.1/website/about, and I tried to do that with the last line. However, if I now try to go to 127.0.0.1/website/anything, where anything is any string, it redirects me to 127.0.0.1/website/about?page=0. In addition to this, I would expect 127.0.0.1/website/about to be handles fine, because of the Last flag at the first rewrite rule, but this redirects me to the same bad url.
I am clueless of why this does not work, and confused, because to me it seems like it is skipping the Last flag. I hope someone can point out what I am doing wrong. Thanks in advance.
last rule rewrites everything so pattern must be more specific
RewriteRule !^index\.php$ about [L,R]
This might work:
RewriteEngine On
RewriteBase /website
RewriteRule ^about$ index.php?page=0 [L,NC]
RewriteRule ^portfolio$ index.php?page=1 [L,NC]
RewriteRule ^cv$ index.php?page=2 [L,NC]
RewriteRule ^contact$ index.php?page=3 [L,NC]
RewriteCond %{REQUEST_URI} !^/cv [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteCond %{REQUEST_URI} !^/contact [NC]
RewriteCond %{REQUEST_URI} !^/portfolio [NC]
RewriteRule ^.*$ /about [R,L]
It will redirect everything that isn't /cv, /about, /contact or /portfolio to /about.

mod rewrite .htaccess expressionengine remove index.php and a specific subdirectory

Hate to ask this question, but I've been banging my head on the desk for a while and can't seem to get it. I'm using ExpressionEngine, and I'm using mod_rewrite to remove index.php from all of my URLs. That works fine. Additionally, I want anything that is myurl.com/adwords/(anything) to be rewritten to myurl.com/(anything). My regex and htaccess skillz are weak. Here is what I have in .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/adwords/(.*)$
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
So I think I'm saying anything that ends in /adwords/(something), capture the something, and then append it at the end of index.php via $1. I'm guessing this is simple. Thanks!
The rule you are looking for as such is probably simply
RewriteRule ^adwords/(.*)$ index.php/$1 [L]
with no rewrite condition needed, but I wonder... do you really want to rewrite http://myurl.com/adwords/foo to http://myurl.com/index.php/foo, rather than to http://myurl.com/index.php?a=foo ?
If you also want to rewrite something like http://myurl.com/baa/adwords/foo to http://myurl.com/baa/index.php/foo, you have to omit the ^:
RewriteRule adwords/(.*)$ index.php/$1 [L]
BTW, you can test most of your rewrite rules here: http://htaccess.madewithlove.be/

Is this htaccess rewrite correct?

I have the following in my htaccess file. What I'm trying to do is to make
domain.com/index.php/view/whatever accessable via domain.com/whatever
and also redirect from non www to www.
This works for all urls that have index.php/view in them but now other URLs that don't have index.php/view in them are breaking not working. Ex: domain.com/index.php/site/pages no longer works since it doesn't have index.php/view in it.
I want htaccess to only affect those URLs that have index.php/view in them and not anything else. What do I need to do to fix that?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/view/$1 [L]
RewriteCond %{http_host} ^domain.com [nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
UPDATE. To narrow things down, How can i have both rules like so. i need them both
RewriteRule ^(.*)$ /index.php/view/$1
RewriteRule ^(.*)$ /index.php/site/$1
So if a request is made to something like http://domain.com/index.php/site/pages, you want it to pass through untouched, but if it's something like http://domain.com/whatever, you want it to get rewritten?
The first rule you have there matches everything (except files and directories). You probably want to narrow the RewriteRule with something like:
RewriteRule !^index.php /index.php/view/$1 [L]
HTH
Neal

How can I use .htaccess rewrite to redirect root URL to subdirectory?

Trying to get
www.example.com
to go directly to
www.example.com/store
I have tried multiple bits of code and none work.
What I've tried:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
What am I doing wrong?
You can use a rewrite rule that uses ^$ to represent the root and rewrite that to your /store directory, like this:
RewriteEngine On
RewriteRule ^$ /store [L]
I was surprised that nobody mentioned this:
RedirectMatch ^/$ /store/
Basically, it redirects the root and only the root URL.
The answer originated from this link
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]
If you want an external redirect (which cause the visiting browser to show the redirected URL), set the R flag there as well:
RewriteRule ^$ /store [L,R=301]
Here is what I used to redirect to a subdirectory. This did it invisibly and still allows through requests that match an existing file or whatever.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]
Change out site.com and subdir with your values.
To set an invisible redirect from root to subfolder, You can use the following RewriteRule in /root/.htaccess :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]
The rule above will internally redirect the browser from :
http://example.com/
to
http://example.com/subfolder
And
http://example.com/foo
to
http://example.com/subfolder/foo
while the browser will stay on the root folder.
Another alternative if you want to rewrite the URL and hide the original URL:
RewriteEngine on
RewriteRule ^(.*)$ /store/$1 [L]
With this, if you for example type http://www.example.com/product.php?id=4, it will transparently open the file at http://www.example.com/store/product.php?id=4 but without showing to the user the full url.
This seemed the simplest solution:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]
I was getting redirect loops with some of the other solutions.
Most of the above solutions are correct but they are all missing the transparency of the redirection.
In my case, when visiting www.example.com I wanted to get redirected to the subdirectory /store but without updating the URL to www.example.com/store. (all I want is to get the page code form that directory). If that is your case the solution below works perfectly.
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /store/$1 [L]
source: http://wiki.dreamhost.com/Transparently_redirect_your_root_directory_to_a_subdirectory
I don't understand your question...
If you want to redirect every request to a subfolder:
RewriteRule ^(.*)$ shop/$1 [L,QSA]
http://www.example.com/* -> wwwroot/store/*
If you want to redirect to a subfolder which has the domain name
RewriteCond %{HTTP_HOST} ([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ %1/$1 [L,QSA]
http://www.example.com/* -> wwwroot/example.com/*
I have found that in order to avoid circular redirection, it is important to limit the scope of redirection to root directory.
I would have used:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,L]
Formerly I use the following code which is work correctly to redirect root URL of each of my domains/subdomains to their correspondence subdirectories which are named exactly as the sub/domain it self as below:
RewriteCond %{HTTP_HOST} ^sub1.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain1.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2.domain1.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub1.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub1.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub2.domain2.com
RewriteCond %{REQUEST_URI} !subs/sub2.domain2.com/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
However when I want to add another subs or domains then it will need to be added in the above code. It should be much more convenient to simplify it to work like wildcard (*) as below:
RewriteCond %{HTTP_HOST} ^sub
RewriteCond %{REQUEST_URI} !/subs/
RewriteRule ^(.*)$ subs/%{HTTP_HOST}/$1 [L,QSA]
So whenever another subdomains/domains is added as long as the subdomain name has a prefix of sub (like: sub3.domain1.com, sub1.domain3.com etc.) the code will remain valid.
Two ways out of possible solutions to achieve this are:
1. Create a .htaccess file in root folder as under (just replace example.com and my_dir with your corresponding values):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_dir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_dir/index.php [L]
</IfModule>
Use RedirectMatch to only redirect the root URL “/” to another folder or URL,
RedirectMatch ^/$ http://www.example.com/my_dir
I think the main problems with the code you posted are:
the first line matches on a host beginning with strictly sample.com, so www.sample.com doesn't match.
the second line wants at least one character, followed by www.sample.com which also doesn't match (why did you escape the first w?)
none of the included rules redirect to the url you specified in your goal (plus, sample is misspelled as samle, but that's irrelevant).
For reference, here's the code you currently have:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sample.com$
RewriteRule (.*) http://www.sample.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.sample\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
One can use Redirect too for this purpose
Redirect 301 / www.example.com/store
Or Alias for mapping
Alias / /store
Edit: mod_alias is only applicable in httpd.conf.
Refrences
http://httpd.apache.org/docs/2.2/mod/mod_alias.html
https://httpd.apache.org/docs/trunk/rewrite/avoid.html
A little googling, gives me these results:
RewriteEngine On RewriteBase
/ RewriteRule ^index.(.*)?$
http://domain.com/subfolder/
[r=301]
This will redirect any attempt to
access a file named index.something to
your subfolder, whether the file
exists or not.
Or try this:
RewriteCond %{HTTP_HOST}
!^www.sample.com$ [NC]
RewriteRule ^(.*)$
%{HTTP_HOST}/samlse/$1 [R=301,L]
I haven't done much redirect in the .htaccess file, so I'm not sure if this will work.
try to use below lines in htaccess
Note: you may need to check what is the name of the default.html
default.html is the file that load by default in the root folder.
RewriteEngine
Redirect /default.html http://example.com/store/
you just add this code into your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]
</IfModule>
This will try the subdir if the file doesn't exist in the root. Needed this as I moved a basic .html website that expects to be ran at the root level and pushed it to a subdir. Only works if all files are flat (no .htaccess trickery in the subdir possible). Useful for linked things like css and js files.
# Internal Redirect to subdir if file is found there.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/subdir/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /subdir/$1 [L]
I'll answer the original question not by pointing out another possible syntax (there are many amongst the other answers) but by pointing out something I have once had to deal with, that took me a while to figure out:
What am I doing wrong?
There is a possibility that %{HTTP_HOST} is not being populated properly, or at all. Although, I've only seen that occur in only one machine on a shared host, with some custom patched apache 2.2, it's a possibility nonetheless.

Resources