htaccess subdomain wildcard to subdirectory single file handler - .htaccess

My website handles every request on the index.php file
https://mywebsite.com/a
https://mywebsite.com/a/b
It doesn't matter, https://mywebsite.com/index.php is used. That is done using:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} !\.(css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php [L,QSA]
Now, what I want as well is that all subdomains (*) are directed to https://mywebsite.com/*/
So: https://a.mywebsite.com/b/c/d -> https://mywebsite.com/a/b/c/d
I tried adding:
RewriteCond %{HTTP_HOST} ^(.+)\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ https://mywebsite.com/%1/$1 [L,R=301,QSA]
But I can't seem to get this to work, neither do I have enough skills to know what I'm doing exactly in this case. Could anyone provide me any tips and explain what is doing what exactly?
My index.phpphp extracts the URL using:
$_SERVER['REQUEST_URI']
Then, I explode all the components into an array with the '/' delimiter. I would then want to obtain an array with ['a', 'b', 'c', 'd'] when I navigate to https://a.mywebsite.com/b/c/d
When I currently implement this, and navigate to https://a.mywebsite.com, I get a "server not found" message (firefox)

The rules you have defined are correct for your purpose. This online service at htaccess.madewithlove.com confirms this, see picture below.
The issue in your case is that the subdomain a.mywebsite.com is not configured properly. You want to login into your domain registrars' dashboard and create a CNAME record that would point from a.mywebsite.com to mywebsite.com.
After adding the DNS record and waiting a few hours, you can verify it with dig or some other dns lookup tool or just open the url in a browser.
It should look like this:
$ dig a.mywebsite.com #1.1.1.1 CNAME +short
mywebsite.com.
The rules are fine (I'm not affiliated with: htaccess.madewithlove.com):

Related

.htaccess cascading conditions

I'm trying to figure out how to manage potentially conflicting conditions in .htaccess
My setup is the following:
- I have a CMS running on a server that can be accessed through myCMSdomain.com where myCMSdomain.com would be CMS home page and myCMSdomain.com/admin would be the admin interface.
- Sites using this CMS should be pointing to myCMSdomain.com/sites/index.php
- Images for all sites are available somewhere behind myCMSdomain.com/admin/images/sitename/...
So here is how I tried to tackle this problem:
RewriteCond %{HTTP_HOST} !^(www.)?myCMSdomain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/index.php
With this, all incoming requests coming from other domain names are treated correctly by the index.php file but as images are hosted somewhere else, I'd like to use a rule saying that images should be fetched somewhere else like this:
Visible url format for images is: images-[sitename]/[image_path]
Real location of the images is: admin/site/[sitename]/[image_path]
The following rule works but not in combination with the first rule
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ admin/site/$1/images/$2
Images end up calling index.php instead of using the rule I defined for them.
I have tried to excluse the image directory from the conditions but it doesn't work either:
RewriteCond %{REQUEST_URI} !(images-([a-zA-Z0-9]+)/(.*))
I might have similar issues in the future with other exception so I was wondering if there was a way to handle this.
Thanks!
Laurent
UPDATE 1:
If I use the following rule on top of all other
it works only if I'm using myCMSdomain.com domain name
if I use any other domain like anotherdomain.com, the rule leads to a http 500
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ manager/site/$1/images/$2
So http://www.myCMSdomain.com/images-test/test.jpg leads me to the correct image
But http://www.anotherdomain.com/images-test/test.jpg leads me to a 500 http error code while this domain is pointing correctly to sites/index.php
UPDATE 2:
On Justin's request, here is a view on the physical directory structure on the server
/admin/
/admin/site
/admin/site/site_name/
/admin/site/site_name/images/
/sites/
/sites/js
/sites/css
You can rearrange your conditions logic.
# if www.myCMSdomain.com or myCMSdomain.com -> do nothing
RewriteCond %{HTTP_HOST} ^(?:www\.)?myCMSdomain\.com$ [NC]
RewriteRule ^ - [L]
# if we reach here, this means it's a subdomain/another domain
# images rule
RewriteRule ^images-([^/]+)/(.+)$ /admin/site/$1/images/$2 [L]
# not a file/directory -> sites/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /sites/index.php [L]
It is also possible the way you did but it would be longer to write.
Also, keep in mind that conditions (RewriteCond) are non-cumulative: they are for next rule (RewriteRule) only.
I've been struggling with this issue for some time now but at last I think I have found the issue. It looks like there was something wrong in the domain name configuration, I don't know exactly what but once I had re-saved the mapping of all domains, the htaccess worked the way it should.
In the end I have used Justin's proposition, it looks more future proof than mine.
Thanks for your help and happy new year to all
Laurent

Use htaccess to redirect root but still have directory listing accessible with a backdoor

What I thought I was trying to do was genius, but it seems to not work properly.
I have a public subdomain with temp files on it. I don't want people to be able to see all the files listed when they visit it. I don't want to password protect it because that will mess up several scripts when fetching files off the server, and I don't want a blank index.html there to stop listing because I want to be able to see the listing.
So my stroke of genius was to have a .htaccess file like so:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/#dir
RewriteCond %{REQUEST_URI} !/?show=dir
RewriteRule ^$ http://example.com/ [nc]
My theory was that visiting files.example.com would redirect to example.com but visiting files.example.com/?show=dir or files.example.com/#dir would not; and obviously since ?show=dir does nothing it would list all my files as normal.
My RewriteConditions have no effect though.
Is this possible?
First, you can't match against URL fragments (the #dir part) because that doesn't ever get sent to the server. It's a client side only thing.
Second, you can't match against the query string (the ?show=dir part) in the %{REQUEST_URI} variable, you need to use %{QUERY_STRING} instead.
So try:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^show=dir$
RewriteRule ^$ http://example.com/ [nc]

Tricky: Remap url but stay on the same url internally

Need your help. Just spend many ours on this htaccess problem and still don't have a clue how to manage this.
I have many http://www.example.com/menu-alias/foo links on my company's website which should get redirected to http://www.example.com/foo.
This alone shouldn't be the hard part but listen up... the tricky part follows.
I don't manage to get the site (Joomla 1.5) working without the 'menu-alias' this means that all http://www.example.com/foo should get internally mapped to http://www.example.com/menu-alias/foo. So that the user still has http://www.example.com/foo in his browser's address bar.
To make it even more complicated i have to 301 redirect the old menu-alias/foo links to /foo.
Can some htaccess guru help me out? Is this even possible?
You can try adding these rules in the htaccess file in your document root (or vhost config):
RewriteEngine On
# externally redirect requests that have "menu-alias"
RewriteCond %{THE_REQUEST} /menu-alias/([^\ \?]+)
RewriteRule ^ /%1 [L,R=301]
# internally rewrite requests back to menu-alias
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/menu-alias/
RewriteRule ^/?(.*)$ /menu-alias/$1 [L]
Couple of potential problems:
Joomla may be looking for the original un-rewritten request in $_SERVER, if so, the rewrite won't work.
The rule to add the /menu-alias/ back into the URI does so blindly rewrites all requests that don't point to an existing resource. This means "virtual" paths that Joomla may handle will get a "menu-alias" appended to the front.

.htaccess redirecting domain alias'

I have a client that has a good amount of domain alias' and wants them all redirected to the one main domain on the site. They also want to know which of the domain alias' is doing the redirecting. I have that part down but I want to optimize the code to the best most proper way it should be and to eliminate the amount of code I have to write. I am wanting to know if there is a way to pass to the RewriteRule url the domain alias that was used.
This is what I have now. I am looking for the domain alias that is being hit and then passing that alias to the url. Then in google analytics I can see how many times that url was used to hit the page.
RewriteCond %{HTTP_HOST} ^(www\.)?domain-alias1\.com [NC]
RewriteRule ^(.*) http://www.main-domain.com/?domain-alias1\.com$1 [R=301,L}
But my goal is to not have to write both the condition and rule for every single domain alias.
Is there a way to see which alias was hit and then have the rewrite rule automatically add that to the position I have specified?
I had originally tried something like this just to see if it would work(although I have tried many different ways):
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.com [NC]
RewriteRule ^(.*) http://www.main-domain.com/?$1\.com$2 [R=301,L]
You can try something along these lines:
RewriteCond %{HTTP_HOST} !^(www\.)?main-domain\.com$ [NC]
RewriteRule ^(.*) http://www.main-domain.com/$1?domain=%{HTTP_HOST} [R=301,L]
With this any request NOT for domain www.main-domain.com will be redirected to www.main-domain.com with the domain name in query string domain.

htaccess subdomain rewrite

It is many topics here about subdomains but no one can help me...
I use htacces to set subdomain to folder
So if we put http://en.example.com/something
I use something like this..
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://example.com/%1/$1 [NC]
This works fine but adress in bar is changed to http://example.com/en/something but I want keep http://en.example.com/something
so I tried this
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^([^.]+)\.example\.com(.*) /$1/$2
or just
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ %1/$1 [NC]
but this doesn't work. Any solution or ideas ?
One solution is use language there (http://example.com/en/something) where I rewrite it but after If I work on subdirectories I get something like http://example.com/subdirectory/en/something - terrible.
Maybe I like http://example.com/en/subdirectory/something bud how proceed this...
And also on some private servers first one case send me to "maybe" default domain, so it is not working for me. (maybe this is some server condition or settings)
I know this is a month late, but maybe this will still be useful for somebody. A few things here:
Regarding your first RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://example.com/%1/$1 [NC]
As it seems you already discovered, rewriting to another URL will also redirect the user's browser to that new URL, even if it's at your own domain. To keep it hidden, you have to rewrite to a file path on the server (like you do with your next two rules).
Regarding your second RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^([^.]+)\.example\.com(.*) /$1/$2
The problem there is that you can't match the domain name in the RewriteRule, only the URL path. If your URL is www.example.com/something/somethingelse, the string you're trying to match is just something/somethingelse. In other words, it excludes www.example.com/, so this RewriteRule pattern you have will never match the domain name because the pattern isn't even being tested against that part of the URL, but you included the domain name in the pattern, causing the match to fail.
Regarding your third RewriteRule:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ %1/$1 [NC]
This looks like it should work, so I can't say for sure why it isn't without knowing more about how your files are organized on the server and so forth. Let's say you have all of the website's files in /home/somebody/public_html/. In order for the RewriteRule to work as it is right now, you would need to have an en subdirectory in public_html. So, if somebody went to en.example.com/something, the RewriteRule would cause Apache to serve the file at /home/somebody/public_html/en/something. My guess why it's not working for you is that you might have the subdomain pointing somewhere other than public_html (assuming you actually had the website files organized like in my example). Remember that what you're rewriting to (/$1/$2 in this case) is a file path on the server, not a URL to your website.
I hope that helps! You may have already solved this by now, but even if you have, I'm hoping other people will still find this useful.

Resources