In the root folder of my hosting I have an htaccess file with, among other things, the following code which redirect to https and www.:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In the /news/ folder i have entry.php file which have for example ?slug=this-is-first-entry. I want it to look like this https://www.example.com/news/this-is-first-entry.
So I want redirect this https://www.example.com/news/entry.php?slug=this-is-first-entry to this https://www.example.com/news/this-is-first-entry
I have this code in .htaccess in /news/ folder:
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
It's working fine, but redirecting https and www from root folder does not work. Please help, I am not familiar with htaccess.
This is a THUMB rule. If current directory has a .htaccess with RewriteEngine ON then it overrides parent .htaccess directives. If you want parent .htaccess rules to be applied before then use following option:
RewriteOptions InheritBefore
Before RewriteEngine On line.
So your htaccess file inside /news/ folder will become like:
RewriteOptions InheritBefore
RewriteEngine ON
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
Additional suggestion: In case you are trying to rewrite non existing files and directories if this is the case then have your htaccess in your news folder like as follows.
RewriteOptions InheritBefore
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
I need to redirect all pages under a specific folder to another domain with different sub folders
For example:
FROM: https://www.my.domain.com/user/myuser/test.png
TO: https://www.newdomain.com/all/allegato1.png
I have try many htaccess file but all not work
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my.domain.com$ [OR]
RewriteCond %{HTTP_HOST} www.my.domain.com$
RewriteRule ^user/myuser/(.*) http://www.newdomain.com/all/$1 [R=301,L]
I insert my .htaccess into the dir my.domain.com/user/myuser/
How can i made this rule? Thanks for your help
Inside /user/myuser/.htaccess following rule should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?my\.domain\.com$ [NC]
RewriteRule .* http://www.newdomain.com/all/$0 [R=301,L]
Looked on here at all the answers regarding this issue but none fixes my issue. I have a directory called pdfs which used to contain all my .pdf files. They are now all inside pdfs/sales so trying to create a redirect for all files in the pdfs directory to look inside the pdfs/sales directory instead using a .htaccess file in the root of my site. Everything I've tried so far results in an infinite loop. Here's what my .htaccess looks like so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^pdfs/(.*)$ /pdfs/sales/$1 [R,NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
The first rule redirects all www. traffic to non-www. url. The second rule is my pdfs rule. The last rule is for redirecting all requests to index.php for seo friendly urls. Is there a conflict here? Where am I going wrong?
Thanks
You can keep your rule like this (my comment inline):
DirectoryIndex index.php
RewriteEngine on
# remove www from domain name
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# redirect /pdfs/abc.pdf to /pdfs/sales/abc.pdf
RewriteRule ^pdfs/((?!(?:sales|rent)/).*)$ /pdfs/sales/$1 [R=302,NC,L]
# for all non-files and no-directories route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?/$1 [L]
Make sure to test it after clearing your browser cache.
I would like to modify my .htaccess file so that when someone comes into my site without typing www the site always redirects them to the www version. For example, if my url is www.abc.com and they just type abc.com, I want to redirect them to abc.com.
Here is my current htaccess file:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Normally I know how to do the redirect, but im having issues since it already has those few lines in there.
I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
(edited to have all the code in the same block)
Add something like this immediately after RewriteEngine on:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
There are two methods available
1) use mod_alias apache module
Redirect permanent /something http://yourwebsite.com/something
2) Add the following entry in your .htaccess / http.conf / yourwebsite.conf in the webserver configuarion directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule ^(.*)$ http://www.yourwebsite.com$1 [R=permanent,L]
If you want redirect example.com to www.example.com you can try below code
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
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.