I have a big problem that causes my whole web root to shut down (505 Internal Error) where every domain is affected, even if the htaccess file is only in one directory. My file structure is organized this way, so every domain is a seperate folder:
web root
-- [domainA]
(domainA files like the .htaccess file)
-- [domainB]
-- [domainC]
(web root files)
I want to implement my routing system and everything works fine on my localhost with this .htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
However, I'm testing for hours to make my routing work on my remote server with the same .htaccess code. I uploaded the file and it also works when I'm at the main directory like www.domainA.com.
BUT every link I'm clicking on my website like www.domainA.com/signup leads to an 505 Internal Server Error. The whole domain isn't even responding for a long time. I have to delete the .htaccess file from my remote server and maybe after 15-30 minutes it's reachable again. That also makes this problem so difficult to debug. Also my whole web root seems to be affected since all other domains aren't reachable, too (even if the .htaccess file is only located in the seperate domain folder like in [domainA]).
Then I tried to apply my rule only on a specific user agent named fake-user:
RewriteCond %{HTTP_USER_AGENT} ^fake-user$
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
This way it surprisingly works like on localhost and I don't get why. Obviously I don't want to limit my audience to user agents named fake-user to interact with my website. So currently all other users would "crash" my website. I hope to get some explanations what could cause this and how to fix this problem to make it work like on my localhost for all users?
Sounds like it could be recursion, try replacing your rule with this:
RewriteRule ^(?!index\.php$). index.php [NS,L]
which only rewrites when it's different.
Related
I have recently completed my first laravel site, but now I am stuck with deployment. This is an entirely new concept for me. My webspace is supplied by 1&1.
I have attempted several tutorials, but none seem to work.
Based on the tutorial here at:
Uploading Laravel Project onto Web Server
I structured my server folders like so:
(note when first FTPing my server, there was no www or html_docs, only a logs folder).
In the www, my index.php was altered to:
require __DIR__.'/../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/start.php';
In the laravel/bootstrap/paths.php file I altered:
'public' => __DIR__.'/../../www',
When visiting my domain in a browser, for example: myDomain.co.uk - the site is redirected to a "placeholder" (named /defaultsite) page for 1&1 domains and states "This domain name has just been registered."
If I append /www/index.php for example, I get file does not exist, even though the file is residing in my structure above.
As you probably noticed I am very new to server side aspects such as FTPing ect. I have read a few tuts, and all seem to take a different approach, leaving me confused to the best method.
I am not sure where to go from here, so any advice is appreciated. Thank you.
Edit:
I think I actually got it working, but now when I go to domain.com/public I get what I think is a DB error:
SQLSTATE[HY000] [2002] No such file or directory.
Any Advice please?
The reason you are seeing /public/index.php is because you are pointing to the top level directory and not the /public directory. To fix this go to:
Manage domains
Expand your domain and click "Edit Destination"
Change the destination directory to /public/.
This should remove public from the URL.
This is the .htaccess file I use for 1and1 and laravel 4.2 and it removes .index.php and also uses an updated version of PHP:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
AddHandler x-mapp-php6 .php
AddType x-mapp-php6 .php
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>
All fixed :) turns out 1&1 do not host their db name as local host, or 121.0.0.1. I simply changed the details and works a dream. next step is to get rid of the terrible /public/index url.
I know there are TONS of questions on this website regarding this topic, as well as TONS of tutorials around google. However I have searched for a while now and I cannot seem to find one that explains my specific situation.
I am building a social application in which the whole application is being stored in a directory of a subdomain the website like so: subdomain.example.com/network/ <-- network being the directory of all the application files.
I have my .htaccess file located in the root of the subdomain and as of right now I have it removing .php from all the files and that is working throughout the entire project as it should. I have a user.php page which each user's profile is based off and the unique identifier for each user is their username (not 'id' like most people use.) So right now the urls look like /network/user.php?username=TylerB which is not very pretty. I have tried many different things to get this to look like /network/user/TylerB but nothing I try seems to work. I don't know if it's because it's in a directory, a subdomain or what. As of right now, when I have /network/user/TylerB (TylerB is my username) it gives me a 404 error.
Here is my current .htaccess file:
RewriteEngine On
RewriteRule ^user/([^/\.]+)/?$ user.php?username=$1 [L]
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
I would recommend setting the RewriteBase directive and matching on the full pattern, making sure to rewrite to the /network/ directory.
RewriteEngine On
RewriteBase /
RewriteRule ^network/user/([^/\.]+)/? /network/user.php?username=$1 [L]
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
Using the testing tool at http://htaccess.madewithlove.be/ I get an output of:
This rule was met, the new url is http://example.com/network/user.php?username=name
The tests are stopped because the L in your RewriteRule options
Alright, so I've downloaded a CMS for a server I'm setting up and I have some difficulty with the path the files are on.
I have put the CMS in a subdirectory on my server as the root directory is already being used by another CMS. However, the CMS repeatedly uses "/" in the code to link to their files. For example, an image is found using this code:
<img src="/images/banner.png" />
As you know this will not work, because the link above redirects the request to the images folder in the root of the server, not the subdirectory. The CMS also came with a ".htaccess" file, so I immediately thought that I could redirect all requests made using the "/" character to the subdirectory on the server. This is the original ".htaccess" file:
RewriteEngine On
RewriteRule ^index.php(|/)$ content/.php
RewriteRule ^error.php(|/)$ content/error.php
RewriteRule ^housekeeping(|/)$ housekeeping/index.php
RewriteRule ^(|/)$ content/$1.php
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ content/$1.php
RewriteRule ^rd/([^/]+)(|/)$ /rd.php?id=$1
RewriteRule ^quickregister/start(|/)$ /register/start.php
RewriteRule ^quickregister/step2(|/)$ /register/step2.php
RewriteRule ^quickregister/step3(|/)$ /register/complete.php
RewriteRule ^community/online(|/)$ content/online.php
RewriteRule ^community/vip(|/)$ content/vip.php
RewriteRule ^credits/goldvip(|/)$ content/goldvip.php
RewriteRule ^home/(..*)$ content/home.php?u=$1
RewriteRule ^articles/(..*)$ content/articles.php?story=$1
ErrorDocument 403 /content/error.php
ErrorDocument 404 /content/error.php
I thought that by adding
RewriteRule ^/$ /subdirectory/
the requests to "/" would be redirected, but to no avail. I have checked the apache configuration and it seems that overwriting the config using htaccess files is enabled, so if anyone is able to help me out I'd be very happy.
Thanks in advance!
EDIT:
I came real close to a solution. I inserted this code just below "RewriteEngine on":
RewriteRule ^(.*)/(.*)$ /private_servers/strebbohotel/$2
This returned the following error page when visiting the url "http://mysite.com/private_servers/strebbohotel/content/.php":
Not Found
The requested URL /private_servers/strebbohotel/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
As you can see, it skips the "content" subdirectory for some reason. I believe it has something to do with the other lines of the .htaccess file but I can't figure out which ones. It also could be that I need a more complex regex, but I'm not particularly good with it so if someone could help me further I'd be thankful.
You have to change the .htaccess file on yout root directory - and that will mess with the other CMS. The only solution that comes to my mind is to use combination of RewriteCond's:
RewriteCond %{HTTP_REFERER} my_better_cms_url_regex
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^.*$ /subdirectory/%{REQUEST_URI}
That should do the trick. If you want to customize it, refer to this cheatsheet (or simmilar). But be aware, that .htaccess in root directory is checked against ALL requests made to your subdir cms - therefore the need for second condition.
The .htaccess file in your CMS subdirectory would have no effect on requests made against the server root directory. You would need to add an .htaccess there.
Even then though, I don't know how you would differentiate requests from the CMS (like the image request in your example) from those intended for the application in the web root.
Does your CMS not have any configuration setting that allow you to specify a path the the files other than the web root?
You want to use:
RewriteBase /another_root_dir/
in the beginning of your .htaccess file
It's quite common for us to put in progress sites at www.domain.com/dev/ and then once the client has signed off the site to move it to the top level www.domain.com, what we like is to be able to put a .htaccess file in the top level so that once we've moved the site out of /dev if the client accidently goes to www.domain.com/dev/apage.php that they be redirected to www.domain.com/apage.php, but only if www.domain.com/dev/apage.php doesn't exist.
Sometimes the dev folder will be called various other things, and ideally we don't want to have to edit the .htaccess file to match the folder name.
Thanks for any help.
You could do something like
Edited based on comments:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule dev/(.*)$ $1 [R=301,L]
Which basically means that if the file doesn't exist - RewriteCond %{REQUEST_FILENAME} !-f - then rewrite any request to dev back to the root. You have to specify dev/ in the rewrite rule as otherwise you will get stuck in a redirect loop.
This will only work however if you are using explicit files rather than a framework with everything routed through index.php for example
I am trying to rewrite my urls to go from /controller/method to index.php?/controller/method, and that is mostly working, except that forms won't submit correctly. I have some routing set up, but the forms are also breaking on non re-routed urls. Here's a little context.
I have moved my index.php out into a separate web folder that is the web root, so there is no chance of exposing application files. I'm not sure whether this would potentially cause this problem, but it's something slightly unusual about my installation. My directory structure looks like:
+ codeigniter/
+ application/
- models, views, etc
+ system/
+ web/
- .htaccess
- index.php
- css, javascript, images, etc
My .htaccess is pretty simple:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
It seems to work fine on XAMPP for Windows, but it is broken on a basic Ubuntu LAMP stack. When I submit a form, nothing happens. The page that the form directs to loads, but as if the form hadn't been submitted.
Sorry, one more important piece of information: If I leave the index.php in there, so the urls look like /index.php/contacts/edit/1, it works.
Let me know if I can provide any more information. Thanks!
Turns out a part of the .htaccess that I didn't include was causing the error to be difficult to find.
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I had assumed that this would show a CodeIgniter error page if it reached that block, but it turns out (I assume) that CodeIgniter was valiantly trying to salvage the situation by looking at the path in $_SERVER, from which it was able to get the page it was supposed to display. However, since the path didn't exist, none of the $_POST data made it through. In the error log I was seeing this line:
[error] File does not exist: /path/to/web/contacts/edit/1
Enabling mod_rewrite solved the problem.