-1

I have multiple domains setup with my webspace (all via a hosting provider)

So www.maindomain.example links to /maindomain on my webspace. www.domain2.example links to /domain2 on my webspace. Pretty simple so far.

However as soon as I place another folder into one of those directories the folder of the domain itself gets mentioned in the URL again.

So imagine this … I have a folder test inside maindomain. In that case the URL to this folder is not www.maindomain.example/test but www.maindomain.example/domain2/test. So the maindomain directory name that is repeated in the url.

Why is that? Is there a way to make that go away via mod_rewrite and .htaccess?

I found this in the .htaccess file on the root of my

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/maindomain/
RewriteCond %{DOCUMENT_ROOT}/maindomain%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/maindomain%{REQUEST_URI} -d
RewriteRule ^(.*)$ /maindomain/$1 [L]

However, since I'm not much of a server-guru I can't figure out what that actually means.

Patrick Mevzek
  • 9,273
  • 7
  • 29
  • 42
Matt
  • 43
  • 1
  • 1
  • 11
  • Did you post the complete `.htaccess` file? There might be other directives and if I don't see them I might give you wrong counsel. – nalply Oct 13 '12 at 14:44
  • Well, this is all! See my comment in your answer underneath! Without those lines the urls wouldn't work. I just want `/maindomain/` not shown in my address. – Matt Oct 14 '12 at 08:12
  • Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Jul 28 '19 at 16:07

1 Answers1

0

The first RewriteCond checks whether the uri does NOT start with /maindomain/.

The second and the third check the existense of the file as a normal file OR as a directory.

Then if all conditions are fulfilled, rewrite in prepending /maindomain/ to the already existing URL.

Now to your problem. You have the directory {DOCUMENT_ROOT}/maindomain/test/ and want it to be reachable from www.maindomain.com/test. This additional lines does the work. Insert it just below RewriteEngine On:

RewriteRule ^test/(.*)$ /maindomain/test/$1 [L]

This takes any URI starting with /test/ and redirects it to /maindomain/test. The [L] part means, stop this round of redirecting here (and don't evaluate the four lines below) and start a new round.

Attention: this is not tested (since I don't have access to your system). URL Rewriting often needs some testing to get it right. StackOverflow is not the right medium to give you support. You need to learn URL Rewriting yourself or hire someone who knows it.

nalply
  • 1,067
  • 1
  • 10
  • 19
  • Well, I see! Without those lines mentioned above the desired url I want doesn't work at all. Right now when calling `maindomain.com/test` it automatically redirects to `maindomain.com/maindomain/test` and works! When commenting out the lines above `maindomain.com/test` leads to a 404. So the lines above are needed for it to work. However I wonder if I can add an additional rule that "hides" the `/maindomain/` directory from the url so that the called url `maindomain.com/test` stays like this in the address bar and WORKS. – Matt Oct 14 '12 at 08:11
  • Thank you for your update! However, this doesn't work for me. Moreover is there no way of defining a "variable" for `test` so I don't have to define a seperate rule for every subdirectory? – Matt Oct 14 '12 at 17:04
  • As I have said, this needs some testing. You are on your own now. I flagged this question as too localized already because your question won't help anybody else. – nalply Oct 14 '12 at 17:07