I have a local Apache server set up on my machine, with wildcard DNS in place. I have it set up so that it works like [foldername].loc. So, for instance, a folder under my htdocs folder called MyDomain, would be accessed via mydomain.loc. This code works fine, and the code in my .htaccess in my htdocs is below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]*\.)?([a-z0-9-]+)\.loc$ [NC]
RewriteCond %3::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule (.*) /%3/$1 [PT,QSA]
Now, the above code also passes through subdomains, such as "john.mydomain.loc". Now, I have the following folder structure in the folder MyDomain:
MyDomain
- active
- index.php
- working
- index.php
.htaccess
In the .htaccess of MyDomain is the following code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteRule ^(.*)$ /active/$1 [L]
What this should do, if I understand correctly, is take http://live.mydomain.loc/ and rewrite it to be http://mydomain.loc/active/. Note that I said rewrite, not redirect.
With the code above, however, I get a message in the Apache error log:
[client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
If I change the .htaccess of MyDomain to read as follows:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /active/$1 [L]
When I use this code, it always comes up with a 403 error, saying I don't have permission to view the folder /mydomain/. If I set Options +Indexes, I only see the folder index of /mydomain. So where along the line is the above code failing?
I have also tried the above code with RewriteCond %{REQUEST_URI} !^/active/. This has made no difference in the results.
I have tried this for over two days, and I can't figure it out. I hope the brilliant minds of StackOverflow can help figure this out. :)
Try setting the rewrite base to where the file actually is:
RewriteEngine On
RewriteBase /MyDomain/
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ active/$1 [L]
And making the /active/ relative: active/
I solved this using the code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/active/
RewriteRule ^(.*)$ active/$1 [L]
The primary fix was RewriteCond %{REQUEST_URI} !^/mydomain/active/. The difference is, the first line I tried, RewriteCond %{REQUEST_URI} !^/active, would match /access/index.php, but not /mydomain/active/index.php.
I hope this helps someone else.
Related
I encountered a small issue while trying to make my site redirect all http requests to https. Before I start, however, I should warn you that I'm rather new to this, so please show mercy ;)
Here's my htcaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://nordicraft.net/%{REQUEST_URI} [NC]
To start off, I'm runing my site on shared hosting, and the everything to the first RewriteRule was in the file the first time I opened it. I have no idea what the -d and -f conditions mean or what are they supposed to do, so I left them untouched. The only things I added were the last RewriteCond and RewriteRule.
Now, the issue is, it works as intended ONLY when accessing the main page, that is, simply entering nordicraft.net in the address bar. It redirects me to the https version, as intended. Also, it does the same thing if I explicitly try to access the index.php file. However, if I try accessing any other website feature, like the forums, it redirects me to https://${REQUESt_URI}. In my case, https://forum. You can view the site in real time at http://nordicraft.net.
Any ideas what is causing this, or possible fixes? Thanks!
Try with:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://nordicraft.net%{REQUEST_URI} [NE,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ /index.php [L]
I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/
I want to have a multi language site. Now, I have 2 domains. The first one is the main domain. That is website.nl. And i have a domain alias, website.org. So the 2 domains share the same public_html folder.
What I want is that:
website.nl will use the file /index.php/$1 and
website.org will use the file /gb/index.php/$1 (So when the url is website.org/test you will use the file /gb/index.php/test (No url redirect)
I found on another topic on stackoverflow the following:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} website.org
RewriteRule ^(.*)$ /gb/index.php [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
But this htaccess file won't work. I will get a 500 error. That's all.
Can someone see what's going wrong?
Your rules are looping, otherwise the 2 rules will mess with each other and loop indefinitely (e.g. requesting /foo will result in /index.php/index.php/index.php/index.php... etc thus returning 500). You need to add some conditions to stop the looping. Try changing the conditions and rules to:
RewriteCond %{HTTP_HOST} website.org
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteRule ^(.*)$ /gb/index.php/$1 [L]
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
This code works. What it does is rewrite's a subdomain so a specific directory will display. Basically it enables wildcard domains to exist.
Currently it does this.
bob.domain.com goes to public_html/-bob
I would like it to go to public_html/__sites/-bob. I tried the code below at the bottom but didn't have any joy. Help would be appreciated.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/-%1 -d
RewriteRule ^(.*)$ -%1/$1 [L]
However it doesn't do what I want it to do.
Instead of it redirecting to root/-directory
I want it to redirect to root/__sites/-directory
I have tried this but I get a server error
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/__sites/-%1 -d
RewriteRule ^(.*)$ __sites/-%1/$1 [L]
How do I tweak this code to get it working?
As I understand you having 500 Internal Server Error. If you check your error log, there will be detailed explanation on the reason that caused it. I'm pretty sure it will be "Number of iterations exceeded" or something like that.
Considering my assumption is correct, this will be the correct rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/__sites/
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/__sites/-%1 -d
RewriteRule ^(.*)$ __sites/-%1/$1 [L]
The error caused by the fact that rewrite is not stopped when you specify the [L] flag -- it only goes to next iteration .. where your current rule will be executed again .. and again.
You have updated destination path from public_html/-bob to public_html/__sites/-bob .. but forgot to update the very first condition: should be %{REQUEST_URI} !^/__sites/.
I'm trying to set up a site that forwards everything but the root directory and index into a variable. I have the htaccess file set up like this right now:
Options +FollowSymlinks
RewriteEngine on
RewriteRule -(.*)$ http://blah.com/blah.php?name=$1 [R,NC]
just so that the index works and anything that starts with a hyphen(-) is rewritten
I would like to be able to have anything that isn't the index file rewritten, and still allow the index file be accessed via blah.com and blah.com/
Any ideas?
Try this :
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/blah.php
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.*) http://blah.com/blah.php?name=$1 [R,NC]
If by any chance you still haven't figured this out, this should work:
RewriteCond %{REQUEST_URI} !^(/|/index.php|/blah.php)$
RewriteRule ^(.*)$ blah.php?name=$1 [R]