Apache Mod Rewrite - Replace www.example.com with www/example.com - apache

First what I have that already works...
On my server I use parked domains where each domain points to the same directory and the software merely determines what the domain is before pulling from the similar named database. For files (images, scripts, etc) I currently have the following setup...
public_html/www.example1.com/
public_html/www.example2.com/
public_html/www.example2.com/
...using the following rewrite...
RewriteCond %{REQUEST_URI} !\.(gif|jpg|js|mp3|png)$
RewriteRule ^[^/]*/(images|search|scripts|sounds)(.+) $1$2 [QSA]
So in example if you request image77.png from example2.com Apache rewrites the (internal) request to...
public_html/www.example2.com/images/image77.png
The external path (in your browser) would simply appear as...
http://www.example2.com/images/image77.png
Secondly, the problem I'm trying to solve...
However as I add more domains this is starting to crowd my *public_html/* directory. What I'm trying to do is simply move all the domain asset directories in to a directory setup that looks like the following...
public_html/www/
...so the www directory would have the following listing...
public_html/www/example1.com/
public_html/www/example2.com/
public_html/www/example2.com/
Lastly, my thoughts on how to possibly approach the problem...
The first Apache variable $1 is generated from %{REQUEST_URI}. If we can simply replace the period . between www and the domain name this should update the rewrite to the newer format though I'm not sure how to do that and keep what works already in tact?
In other words can we can create a rewrite condition to replace just the first period with a slash?

You can capture the domain part after www. and use it later in the RewriteRule as %1
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^.*?/(.*)$ /public_html/www/%1/$1 [L]

Related

htaccess Remove directory from end of URL in apache

Ok, so I know this is a question that has been asked many times, however, I have not been able to find an answer to my particular case, so please do not shoot me down.
I have a website: http://gmcomputers.co.za.
I am redirecting this URL, using .htaccess file, to a subfolder to load the content:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /gmcomputers/ [L,DPI,R=301]
Which works perefectly, except when I go to http://gmcomputers.co.za I get http://gmcomputers.co.za/gmcomputers/.
So my question is, how do I modify the above code to remove the /gmcomputers/ from being appended?
Please note I copied the code above from a website as I am not at all experienced in redirect, etc and am still learning. Also, the reason I am using .htaccess to redirect is due to there being other websites in the root directory and I therefore cannot edit any config files for Apache.
Thanking you.
You contradict yourself in your question. On the one hand you write that you want to redirect and that this "works perfectly", but then you write that you do not want that result.
My guess is that you actually do not want to redirect at all, but that instead you want to internally rewrite your requests to point to that server side folder. While the URL visible in the browser's URL bar does not show that folder. Is that what you are trying to ask?
If so take a look at this example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
You might want to add an actual redirection to direct clients actually using the folder name in their requests:
RewriteEngine on
RewriteRule ^/?gmcomputers/(.*)$ /$1 [R=301,END]
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
Best is to implement such rules in the central http server's host configuration. If you do not have access to that you can instead use a distributed configuration file (typically called ".htaccess") located in the DOCUMENT_ROOT folder configured for the http host, if you enabled the consideration of such files in your host configuration . Though that comes with a number of disadvantages. Above implementation works likewise for both approaches.

htaccess url parameter rewrite for specific page as subdomain

I want to change my specific page URL as subdomain by .htaccess from
example.com/page.php to page.example.com.
We need to make some assumptions, since you've not included this information in the question:
You are requesting (ie. linking to) page.example.com and you wish to serve /page.php.
Both the subdomain page.example.com and domain apex example.com point to the same area of the filesystem, so both hostnames serve the same content.
In which case it is a relatively straight forward internal rewrite from page.example.com to page.example.com/page.php (based on assumption #2 above).
In your .htaccess file, you can achieve this using mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(page)\.example\.com
RewriteRule ^$ %1.php [L]
The %1 backreference simply holds the value page from the preceding CondPattern.
This only matches requests for https://page.example.com/ and not https://page.example.com/<something>

Apache Rewrite: secondary htaccess for domain specific RedirectMatch

On shared web-hosting my software supports multiple domains (all domains point to the same public_html root directory).
What I want to do is keep redirects (and any RedirectMatch) in their own host specific/dedicated .htaccess file.
Visually the directory structure looks like this...
/public_html/ (all domains are pointed internally to this directory)
/public_html/.htaccess
/public_html/www.example1.com/
/public_html/www.example2.com/
/public_html/www.example3.com/
There are two approaches I'm considering though would appreciate input from others:
The first would be to keep domain specific redirects out of the main .htaccess file as defined above. So I'd like to have redirects handled by the .htaccess files as defined by below if possible...
/public_html/www.example1.com/.htaccess
/public_html/www.example2.com/.htaccess
/public_html/www.example3.com/.htaccess
...if this is not feasible I'll settle for a rewrite to a PHP file to hand off redirects to PHP instead. I imagine this isn't as performance oriented though on the other hand it would give me the opportunity to log redirects and see how long it takes them to level off.
Some clarifications:
I'm using shared web hosting so anything Apache related needs to be done through .htaccess files only.
There are no redirects/matches in the master .htaccess file nor will there ever be since two domains may eventually attempt to use the same redirect.
Since you are on shared host, You cannot afford to have any solutions concerning conf files (which BTW are better). So wont bother to list them. Best way to do the above is like this:
The code was written keeping in mind that none of the domains share any kind of file/data on the server. Every file/data pertaining to a domain is kept under a folder having the name equal to its domainname.
The code below is tested(both static and non static):
RewritEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
And add either of the following to the above:
for doing it statically:
RewriteCond %{HTTP_HOST} ^www\.(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /www.%1%2/$1 [L]
for doing it statically: and also if you want to access the site without www
RewriteCond %{HTTP_HOST} ^(www\.)?(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /%1%2%3/$1 [L]
for Non-statically do it: this is a better sol
RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [L]
All the above will do is redirect URI to their specific domain's folder. All other domain specific rewrites can be handled in the respective folders.
If you have URIs without the www, i.e. example1.com change ^www\.(example1|example2|example3)(\.com)$ to ^(www\.)?(example1|example2|example3)(\.com)$

.htaccess rewrite between three servers

I'd like to copy the same htaccess file to three different servers. However I don't want to update the contents each time I do that. How do I make the domain name be detected 'dynamically'?
For example, I have the following:
Redirect 301 /test/directory/page.php http://examplesite.com/original/location.php
This won't work on the other two domains because, obviously, they have different urls.
Should I modify like this:
RewriteRule ^/test/directory/page.php /original/location.php [R=301,L]
Or is there a better way where I don't have to specify the domain name?
Yes, this will work (with one small change -- see notes):
RewriteRule ^test/directory/page.php /original/location.php [R=301,L]
You can also use this (in case you need to change protocol from current HTTP or HTTPS to a specific one):
RewriteRule ^test/directory/page\.php$ http://%{HTTP_HOST}/original/location.php [R=301,L]
NOTES:
%{HTTP_HOST} = current domain name. So if accessed http://examplesite.com/test/directory/page.php then %{HTTP_HOST} will have examplesite.com as its value.
No need for leading slash / after ^ (unless rule will be placed in config file and not .htaccess). For example: when accessing http://examplesite.com/test/directory/page.php the RewriteRule will work with test/directory/page.php.

How do I hide actual directories using mod_rewrite?

I am hosting a couple of domains of the same wordpress installation, now I'd like to have a per-domain folder for some various files I need to put up there.
Essentially I want to map like this:
URL Path
webbfarbror.se/f/* _files/webbfarbror.se/*
grapefrukt.com/f/* _files/grapefrukt.com/*
This little snippet does the job nicely and the RewriteCond let's me enable and disable this on a per domain basis.
ReWriteCond %{HTTP_HOST} webbfarbror.se
ReWriteRule ^f/(.*)$ _files/%{HTTP_HOST}/$1 [L]
However, a file at say, http://grapefrukt.com/f/awesome.jpg is also accessible at it's "real" URL http://grapefrukt.com/_files/grapefrukt.com/awesome.jpg
All my attempts result in infinite redirects back and forth.
How do I disable access through the latter URL?
You can examine the original request as it was sent to the server, which is available as %{THE_REQUEST}. Checking for the /_files/ prefix indicates that the request was of the latter type, and you can then redirect to the appropriate format:
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/_files/
RewriteRule ^_files/[^/]+/(.*)$ http://%{HTTP_HOST}/f/$1 [R=301,L]