0

I have a folder structure like this

/
/project1

I normally use it with one domain (example.com), but now I want to use it as a sepreate domain (project1.example.com) and I am using apache virtualhost. I have succeed to setup, but some problem on the link happened. All the hyperlink in project1 is the path included the "project1" subfolder (e.g. project1/image/pic.jpg). I setup like this

DocumentRoot /doc_root/
Alias /project1 /doc_root/project1/
Alias / /doc_root/project1

RewriteRule /project1/(.*) /$1 [R]

The problem is when i rewrite clean url e.g. example.com/project1/post?id=1 => project1.example.com/post/1 With

RewriteRule ^/post/(\d+)$ /project1/post.php?id=$1 [PT,NC,L,QSA]

I am getting 404 error in the ajax post in the page Since the ajax url is pointed to /project1/process.php And I think this request is not rewriting.

What is the proper way to setup like this? thanks!

tommyvca
  • 51
  • 1
  • 6
  • Redirection means sending the client a new HTTP(S) URL where the document can be found. When possible (own server / VPS) use virtual host configuration and do to not try to implement such a feature in mod_rewrite since it is a performance issue. See also https://httpd.apache.org/docs/2.4/rewrite/vhosts.html and [When not to use mod_rewrite](https://httpd.apache.org/docs/2.4/rewrite/avoid.html). This is the way to go: https://httpd.apache.org/docs/2.4/vhosts/examples.html – Pinke Helga Feb 23 '19 at 09:45
  • Also note that most mass hosting providers implement a feature within the customer web-interface where you can apply additional domains to subfolders. You also will need to handle CORS headers when performing cross-origin request. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – Pinke Helga Feb 23 '19 at 09:50

1 Answers1

0

There's several issues here:

  1. The Alias rules don't look correct and are confusing.
  2. The redirect rule you provided won't extract a query string parameter (i.e. it won't rewrite the path from /project1/post?id=1 to /post/1) nor does it alter the domain name (i.e. it won't change the domain to project1.example.com).
  3. There's no need for the redirect.

I think what you want is something like:

DocumentRoot /doc_root/

RewriteCond %{HTTP_HOST} ^project1\.example\.com$
RewriteRule ^/(.*)$ /project1/$1 [NC,PT]

If you do actually want to redirect, consider:

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/project1/(.*)$ project1.example.com/$1 [NC,R]
Avi
  • 1,231
  • 10
  • 23