This is from a .htaccess located under /~new/
# invoke rewrite engine
RewriteEngine On
# force domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [R=301,L,NC]
When accessing http://domain.com/~new/hello, it is being rewritten to http://www.domain.com/hello
The www. is being added in like it should, but for some reason it is ignoring the /~new/ subdirectory.
Does anyone know what may be causing this? FYI, there is a .htaccess in the TLD but it is empty. I know I could tack on /~new/ to the regex replacement string, but I'd prefer a generic solution (for portability) and I am not sure why it's stripping it out in the first place. I have also tried playing around with RewriteBase but could not get it to work.
Thanks
I think normally the {REQUEST_URI} would be in your rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ www.%1.%2%{REQUEST_URI} [R=301,L]
Related
I have a tricky one and did not find a solution yet after more time spent on this than i'd like to admint.
Is it possible to use .htaccess to show the content of a subdomain in a subdirectory?
Example:
http://sub.example.com/
http://sub1.example.com/
http://sub2.example.com/
should be displayed as
http://example.com/sub/
http://example.com/sub1/
http://example.com/sub2/
Yes, it is possible. This one should work:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/%1 [R=301,L]
I have a partially working .htaccess file and can’t for the life of me figure out what’s wrong.
Here’s the goal: I have example.com whose canonical form I want to be www.example.com. I have that working okay. I also have a subdomain located in the folder /lang/chinese which I want to resolve as china.example.net. This works fine too. Lastly I have the (parked) domain example.net, which I want to be redirected to example.com and to resolve therefore as www.example.com.
It’s this last part that doesn’t work. If I put www.example.net in my browser, that stays in the address bar.
Here’s the relevant portion of my .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} china.example.com [NC]
RewriteRule ^(.*)$ http://china.example.com$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/lang/chinese/(.*)$ http://china.example.com/$1
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,nc,L]
Clearly I am doing something wrong here. How can I fix this?
For this rule:
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,nc,L]
the ^ is telling the regex to match against the beginning of the line. So that means you’re never actually searching for “www” there.
I believe you’ll want to change that to:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net [nc]
Info from Apache:
RewriteRule Directive
RewriteCond Directive
What I think should be simple is just not working.
I have switched domains
Old URL example:
digital.photorecommendations.com/recs/2015/01/big-zoom-field-review/
New URL example:
photorec.tv/2015/01/big-zoom-field-review/
Really just switching domain and dropping the recs folder from the URL
Using http://htaccess.madewithlove.be/ to test and the outputs the correct URL
Options +FollowSymlinks
RewriteEngine on
RewriteBase /recs
RewriteCond %{HTTP_HOST} !^www\.digital.photorecommendations\.com$ [NC]
RewriteRule ^recs(.*) http://photorec.tv/$1 [L,R=301]
When I place this in the htaccess file I get 404 errors on all the pages except the home page. The htaccess file is inside the /recs folder. I have also tried it in the root directory of digital.photorecommendations.com and I get no results at all.
Any suggestions?
Thanks!
You have wrongly used negation in RewriteCond and regex also needs a fix. Use this rule in /recs/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(index\.php)?$ http://photorec.tv/ [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(.+)$ http://photorec.tv/recs/$1 [L,R=301,NC,NE]
This is the code in my .htaccess file which is in the root directory of my shared hosting.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9\-]+)?$ interest.php?interest=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9\_\-]+)?/$ interest.php?interest=$1 [QSA,L]
The problem is, In the same directory lies folders of my add on domains. When I am trying to access them, the 2nd line is creating problem and redirecting it to my primary domain. I want the .htaccess to effect only root folder and not its sub folders. I tried reading other similar questions here and tried changing my htaccess accordingly but the problem remained unresolved.
For example.
its redirecting
mysubdomain.mydomain.com
to
www.mydomain.com/mysubdomainfolder
Have your first rule like this:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Then clear your browser cache and restart your browser.
Well, .htaccess files are designed to apply recursively.
You could try putting a .htaccess in each of the subfolders that correspond to your add on domains with RewriteEngine Off if you want no redirects, or an alternate rewrite rule if you need them.
In addition you may want to add another rewrite condition for the rule, say your add on domain is blah.com you could add
RewriteCond %{HTTP_HOST} !^(.*.)?blah.com
Edit: I should add the new rewrite condition should go below RewriteCond %{HTTP_HOST} !^www.
Edit2: Okay so subdomains then. In which case you basically need to add a Rewrite condition specifying not to apply the rewrite rule if it's a known subdomain. For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2|subdomain3)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now subdomain1.domain.com won't redirect (or subdomain2/3), www.domain.com won't redirect, but anything else will.
I'm trying to redirect all non www. urls leading to my site to www. urls - however, there's one subfolder on my site I don't want changed. The code I've been working with is:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Is there a way to tell this piece of code to ignore domain.com/specialsubfolder?
Thanks a lot, tried searching for this, but couldn't quite find what I'm looking for.
The common way (easy to read and understand) is to add one more condition to ignore this rule if URL starts with /specialsubfolder :
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/specialsubfolder
RewriteRule .* http://www.domain.com%{REQUEST_URI} [R=301,L]
Alternatively, add such condition into matching pattern (more difficult to read but a tiny bit faster):
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(?!specialsubfolder).* http://www.domain.com%{REQUEST_URI} [R=301,L]
P.S. You can add [NC] flags if case-insensitivity is required.