0

I am rewriting sub-folders of my website to sub-domain via bellow code in Apache vhost file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. example.com
RewriteCond %{HTTP_HOST} ^(www\.)?(([^\.]+)\.){1}example.com$
RewriteCond /var/www/example.com/web/crm/build/%3 -d
RewriteRule ^/(.*) /crm/build/%3/$1

so https://example.com/web/crm/folder = https://folder.example.com

Also i am using .htaccess to hide /index.php/ in codeigniter url

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
AddDefaultCharset utf-8

example.com/web/crm/folder works fine

folder.example.com i get 505 internal error which gets fine once i delete lines in .htaccess or just simple access website via folder.example.com/index.php/ (Nothing is showing up in log)

I want to fix rewrite conflict for sub domain

2 Answers2

0

OK, two suggestions:

  1. It would be nice to know what specific server-side error is causing the HTTP 505.

    Please copy/past the exact message from your server log into your post.

  2. It would also be nice to know exactly how your rewrite rules are being interpreted.

    Please enable logging as follows:

How to debug Apache mod_rewrite

Apache Module mod_rewrite

mod_rewrite offers detailed logging of its actions at the trace1 to trace8 log levels. The log level can be set specifically for mod_rewrite using the LogLevel directive: Up to level debug, no actions are logged, while trace8 means that practically all actions are logged.

Example

LogLevel alert rewrite:trace6

George Samarguliani added this comment, which explains the HTTP 505 error:

apache does not start after adding rewrite log rule in vhost file Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

I copied/pasted the vhost snippet above into the first handy Apache config validator, https://htaccess.madewithlove.be/, and got this:

htaccess.MadeWithLove validator

So now at least we know what's causing the server-side error: Apache vhost configuration.

NEW SUGGESTIONS:

  1. As far as the "index.php", I don't even think you need or want a re-write rule.

    Just configure "DirectoryIndex", like this:

    <Directory /myapp>
      DirectoryIndex index.php
    </Directory>
    
  2. As far as the rest of the URL (e.g. ``http://example.com/web/crm/build/`), please update your post with before/after examples of how you would like to the redirect to actually work.

  3. There are lots of good "howtos", with good examples, for using .httaccess/mod_rewrite rules. For example:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Hello, and thank you for your kind reply. apache does not start after adding rewrite log rule in vhost file Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details. reference to server log as mentioned above there are no records – George Samarguliani Feb 17 '19 at 21:05
  • "Apache didn't start... because of vhosts" . Sooooo ... that's an interesting detail! See my updates above – paulsm4 Feb 17 '19 at 21:25
0

Fixed

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /index.php
</IfModule>
  • You're welcome? ALSO: Q: Do you ever expect a circumstance where you *DON'T* have mod_rewrite? Was that really essential? Q: The difference is probably "minimal", and there are pros/cons in either case... but in general, it's more efficient to configure VHost for "DirectoryIndex index.php", and have the user give a URL like "mysite.com/myapp/", than it is to re-write index.php. – paulsm4 Feb 18 '19 at 02:49
  • Hmm, doesn't DirectoryIndex specify index file of the directory ? in codeigniter all the queries are run through index.php like example.com/index.php?/controller and above htaccess just hides index.php from url string. not sure if directoryindex can do that. also as you can see from original question subdomains do not have dedicated vhost files they work as wildcard from example.com vhost file. – George Samarguliani Feb 18 '19 at 14:37