0

I've tried tons (probably in excess of 250) different rewrite rules but none of them are having the desired effect.


Rules for maindomain.com which is a wordpress website with SSL & should run with the prefix www.

  • Check and force www. to the entire domain/folders/subfolders...etc (not just wordpress) EXCEPT the folder testfolder
  • Check and force https. to the entire domain/folders/subfolders...etc (not just wordpress) EXCEPT the folder testfolder

Rules for test.otherwebsite.com which is a custom set of pages hosted in /testfolder/ on the server

  • Check and forcefully remove www. from the domain test.otherwebsite.com
  • Check and forcefully changed https to http from the domain test.otherwebsite.com

These rules shouldn't be needed I don't think... if the first set of rules work correctly.


Ryflex
  • 5,559
  • 25
  • 79
  • 148
  • Are you rewrites working at all? .htaccess needs to first be enabled in apache config, if not already. –  Apr 27 '15 at 01:40
  • Yes, they work fine I just can't figure out the correct set of rewrites (not very good with them yet) – Ryflex Apr 27 '15 at 02:18
  • Honestly, I would just create two .htaccess files, one for each. There's plenty of copy/paste to force https/www –  Apr 27 '15 at 02:37
  • Already tried that, for some reason in the second directory the page returns error 500 because of the htaccess file. – Ryflex Apr 27 '15 at 02:46
  • Check your server logs then for detailed info. Also, why not post your rewrites so others can see and point out the error? –  Apr 27 '15 at 02:50

2 Answers2

0

No need to use rewrite engine if you have access to main configuration files.

(1) Redirect non www to www. A simpler solution is using VirtualHost directive instead of rewrite engine.

<VirtualHost *:80>
    ServerName maindomain.com
    Redirect permanent / http://www.maindomain.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.maindomain.com
    # ... Your www served here
</VirtualHost>

For https, you could just simply copy them and replace the port to 443.

(2) Serve /testfolder in a separate VirtualHost directive

<VirtualHost *:80>
    ServerName test.otherwebsite.com
    # ... anything else
</VirtualHost>

For https, change port to 443.

Note that those won't make exception to /testfolder if accessed from www.maindomain.com/testfolder.

A solution is to move the testfolder outside directory served by www.maindomain.com. For example

/var/www/maindomain <--- directory for www.maindomain.com
/var/www/testfolder <--- directory for test.otherwebsite.com

There is a <DirectoryMatch> directive to prevent that as explained here https://stackoverflow.com/a/214908/779320. But I'm afraid it apply globally and so can't be served in any VirtualHost. But you can test it anyway.

Community
  • 1
  • 1
ihsan
  • 2,279
  • 20
  • 36
0

1) You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule !^testfolder/ https://www.maindomain.com%{REQUEST_URI} [NE,NC,R=301,L]

2) Then you can use this code in your /testfolder/.htaccess file:

RewriteEngine On

RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://test.otherwebsite.com%{REQUEST_URI} [NE,R=301,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • For some reason the test.otherwebsite domain errors out, the .htaccess file isn't working in that subfolder throwing the error: `Invalid command '\xef\xbb\xbfRewriteEngine', perhaps misspelled or defined by a module not included in the server configuration` – Ryflex Apr 27 '15 at 13:52