multiple domains with htaccess rewrite rule - apache

I'm having difficulties getting an htaccess to work with a subdomain.
my server structure:
root / index.php ---//codeigniter index file, for application A, main domain points here.
root/staging/StagingWebsite ---// my subdomain is pointing here.
the folder StagingWebsite has a file called temp.html
moving on, my root htaccess file is this :
RewriteEngine on
RewriteCond
RewriteCond $1 ^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Which turns any access to MyDomain/foo to MyDomain/index.php/foo. (without showing the index.php).
The problem:
When I try to access my subdomain/temp.html, I get a 500 internal error.
when I remove my root/.htaccess, it works fine. So it's obviously my htaccess file.
I've figured out the problem is that the root/.htaccess rule is being applied to the subdomain, which breaks everything, But I have no ideahow to sort it out.
I've placed an empty .htaccess file inside the root/staging/StagingWebsite hoping it would just over-write any previous htaccess settings, But that didn't work.
EDIT
I fixed the issue specifically but I don't like the solution.
I added a RewriteCond to only run the rewrite rule for as specific domain.
Is there a way to solve this without specifying a domain?

Create /root/staging/StagingWebsite/.htaccess with this line only:
RewriteEngine on
This will overwrite any parent's rewrite rules.

Related

Mod rewrite redirect every subdirectory to the same file

I have a website with a products.html file. Now, inside this file, I will have some Javascript code that checks the url to show the correct products/categories. This are some examples of the urls:
example.com/products
example.com/products/
example.com/products/shoes
example.com/products/shoes/
example.com/products/shoes/adidas
example.com/products/shoes/adidas/
example.com/products/shoes/adidas/adidas-predator-20.3
example.com/products/shoes/adidas/adidas-predator-20.3/
So basically, I want that everytime someone goes to the any of the above urls, I want to get the file products.html, I'll manage the rest with JS.
Also I want to be able to test this on my local machine. In this case, my URLs will be:
localhost/websites/myWebsite/products
localhost/websites/myWebsite/products/
localhost/websites/myWebsite/products/shoes
localhost/websites/myWebsite/products/shoes/
localhost/websites/myWebsite/products/shoes/adidas
localhost/websites/myWebsite/products/shoes/adidas/
localhost/websites/myWebsite/products/shoes/adidas/adidas-predator-20.3
localhost/websites/myWebsite/products/shoes/adidas/adidas-predator-20.3/
So I think we would need two htaccess files right?
You may use this single rule that will work on localhost and on production host as well:
RewriteEngine On
RewriteRule (?:^|/)products(?:/|$) products.html [L,NC]
With your shown samples could you please try following, you should place .htaccess file on same level where folder products is present. Also place the products.html in same level too.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(?:products)/?((?:shoes)/?((?:adidas)/?((?:adidas-predator-20\.3)/?)?)?)?$ [NC]
RewriteRule ^(.*)$ products.html [L]
If these are the only rules you will have then change L to END as an additional note here.

htaccess rewrite to https is adding unwanted folder

I don't normally use Apache and I am struggle to get a rewrite rule working.
The root folder contains a folder called public_html, when you browse to the website it loads as expected, I assume Apache automatically finds the index file inside the public_html folder. The problem I have is when adding the rewrite rule from http to https it adds the public_html to the url and a Not Found error is produced.
This is the rewrite rule I was using:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://exampledomain.com/$1 [R=301,L]
The url produced is something like this:
https://www.exampledomain.com/public_html
The desired url would look like this:
https://www.exampledomain.com
There are two parts to this questions:
Should the htaccess file be in the root or the public_html folder?
How do I prevent the rewrite rule adding public_html?
Any help would be greatly appreciated and if you would like further information or feel something is unclear please let me know.
Since this is a shared hosting, there are probably some rewrites already configured in server configuration (like adding /public_html prefix to every request).
You can try to workaround with:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^public_html/(.*)$ https://exampledomain.com/$1 [R=301,L]

apache rewritemap using txt file - match fail

I am trying to establish a rewritemap using a simple txt file for a special project that will ultimately contain about 150 redirects.
I've read through the other posts here and tried to apply alternate mods, but I just can't make a single redirect from this actually work as expected :-/
To confirm my setup:
I am using an include .conf to update my httpd.conf for the proper Virtual host. Upon running /scripts/rebuildhttpdconf (and 'service httpd restart') it is applied without error and the appropriate Include Path reference within httpd.conf becomes uncommented, which tells me it is finding the include file as expected. The Include file is called 'redirecttest2.conf' and contains:
RewriteMap redirect2 txt:/home/example/public_html/redirecttest/redirect2.txt
At the location referenced above, I have the file 'redirect2.txt', which for testing purposes contains:
maintenance.html http://www.example.com/maintenance2.html
maintenance1.html http://www.example.com/maintenance2.html
So my expectation would be that when entering 'example.com/maintenance.html' it would then be redirected to maintenance2.html, etc.
In the .htaccess file at the root of the effected domain, I currently have the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.(js|css|less|png|php|swf|flv|jpg))$
RewriteCond {redirect2:$1} !=""
RewriteRule ^/(.*) ${redirect2:$1} [R=301,L]
However, this does not currently have any effect. Entering example.com/maintenance.html or maintenance1.html does not perform any redirection - with or without the first two conditionals commented out.
I have tried numerous variations, and at times been able to break it altogether (rewrite loop error, whitescreen, or even cases in which the redirect works but instead of going to maintenance2.html, it appends /home/example/public_html to the url - ???) - but at no point can I get the example above to redirect as expected.
I tried getting assistance from the host of the VPS, but they do not provide support for such modifications.
I am hoping someone could look at the above and maybe see something glaring that I am missing? ANY thoughts appreciate...
I was finally able to resolve the issues with my RewriteMap.
In the .htaccess file at the root of the impacted domain, I am now using the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.(js|css|less|png|php|swf|flv|jpg))$
RewriteCond ${redirect2:$1} >"" [NC]
RewriteRule ^(.*) ${redirect2:$1} [R=301,L]
This now appears to work - at least for basic url's.

Redirect to a different domain and keep URL (while removing a segment) with .htaccess

Okay I have been wracking my brain trying to figure this one out.
Here is what I am trying to do:
http://example.com/stuff/uploads/foo/image.png
--- redirect to --->
http://images.example.com/foo/image.png
My current htaccess manages to match a part of the rule and redirect. It doesn't insert the proper part of the path into the new rewrite though, and that's the main hurdle I am facing.
# .htaccess in /stuff/uploads/ directory
RewriteEngine On
RewriteBase /
RewriteCond $1 ^(foo)
RewriteRule ^(foo)(.*)$ http://images.example.com$2 [L,R=301]
So in essence I am trying to:
Match specific directories in the uploads directory.
Redirect to a new domain while preserving part of the original path, but not all.
Any help on this would be much appreciated!
I found the solution.
I just changed the last line to:
RewriteRule ^(foo)(.*)$ http://images.example.com/$1$2 [L,R=301]
The $1$2 at the end adds the URL segment that I want and the file name.
Cheers SO!

Why is my .htaccess file redirecting to full server path instead of relative path?

I've never had a problem with cakePHP before, but something's odd about this server and is causing the redirects in the .htaccess files to behave oddly.
CakePHP uses mod_rewrite in .htaccess files to redirect requests to its own webroot folder. The problem is that the redirects are listing the wrong path and causing a 404 error. My CakePHP application, which is stored in the listings directory, has a .htaccess file as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [R=301,L]
RewriteRule (.*) app/webroot/$1 [R=301,L]
</IfModule>
(*note that the R=301 causes an external redirect so we can see what is going on from our end. It should really omit this flag and do the redirect internally, transparent to end-users)
This is supposed to redirect any request from http://hostname.com/~username/listings/ to http://hostname.com/~username/listings/app/webroot/
However, rather than simply adding “app/webroot/” to the end as it is supposed to, it is adding the full server path ( /home/username/public_html/listings/app/webroot/ ) resulting in the final URL http://hostname.com/home/username/public_html/listings/app/webroot/ which is obviously incorrect and triggers a 404 error.
The hosting is on a shared hosting account, so that limits what I can do with the settings. I've never seen this happen before, and I'm thinking it's something wrong from the hosting side of things, but if anyone has some helpful suggestions then I can put them to the hosting company as well.
The solution to your question can be found towards the bottom of this page in the cakephp book:
For many hosting services (GoDaddy, 1and1), your web server is actually being served from a user directory that already uses mod_rewrite. If you are installing CakePHP into a user directory (http://example.com/~username/cakephp/), or any other URL structure that already utilizes mod_rewrite, you'll need to add RewriteBase statements to the .htaccess files CakePHP uses (/.htaccess, /app/.htaccess, /app/webroot/.htaccess).
I've deployed CakePHP from my profile's public_html folder as well. I had to change 3 the same .htaccess files mentioned above. Just add RewriteBase /~username/ to the .htaccess files just after RewriteEngine on!
Try removing .htaccess from main file... It worked for me
It was quite simple (using uolhost shared host):
Edit both .htaccess files:
/webroot/.htaccess
/.htaccess
Add the following line:
RewriteBase /
Here is the whole /webroot/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]