403 error on htaccess url routing - php

I am new to URL Routing so this issue might be from something which I did wrong. I am using PHP and I need to have simple clean URLs without showing the .php extension in browser. So I used the routing engine from the fat free framework (f3) and added a .htaccess file to the root folder. I followed a tutorial on this and this is the content on my htaccess file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ framework.php [L,QSA]
So i need to redirect all request to framework.php file if no physical file or directory is matched.
Everything was working fine with this, but I recently tried indexing with google and as it turns out robots get a 403 error when the url of the website is given.
For example www.abc.com works fine on browser but gets a 403 error in fetch as google tool. The error is occurring in google page speed insights as well.
When you try to access it as www.abc.com/benefits/ again it works fine on browser but get a 404 error in fetch as google tool.
My robots.txt file contains the following
User-Agent: *
Disallow:
Allow: /
The error make sense if no URL routing is present because I do not have a index.php file and no physical sub directory called benefits. But since I have the rewrite rule I cant figure out why its giving the 403 and 404 errors and why only for robots.
Could any one tell me what’s I am doing wrong? Thanks

As w3d pointed out the problem is not only for the robots and there was nothing wrong with the htaccess file.
The problem was with my main controller which is framework.php
I renamed that to index.php and now the issue has been solved. For some reason even though the url and folder structure is virtual, the server is looking for a index file in the root folder.

Related

Rewrite Rules only work with specific User Agent?

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.

404 Page wont show / no .htaccess Wordpress

I want to show my 404 error page when ever someone gets on a non working / non created page. After a long time of searching on the web I couldnt find any solution so I post one myself. I dont have any .htaccess file either i created it filled it in but in my FTP it kept saying nothing was safed in there and it didnt show any bytes so thats something im also worried of.
So how can i get my .htaccess file to work and show my 404 Error page template? I followed all the steps to make a 404.php file but i dont mind it to be redirected to the same template I made for it but then as a page any of those solutions is fine.
You've got some rewrite rules in your .htaccess file that are preventing a 404 error from every truly happening...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
That basically states that if the requested resource is neither a file nor a directory (basically a 404 error), redirect to /index.php
Try this in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# delete the line below to have /index.php/anything trigger a 404
RewriteRule ^index\.php$ - [L]
</IfModule>
# END WordPress
#404 redirect
ErrorDocument 404 /httpdocs/wp-content/themes/avian/404.php
Note
This may make Wordpress quite unhappy however, I'm not sure how it uses internal routing, it may rely on breaking 404s to determine which pages to load by using fake (RESTful) URLs - reinstating 404 errors make cause Wordpress to stop working properly ... I am, however, no expert on it having barely ever touched it.

htaccess renaming .php files to .html files giving file not found errors?

I'm using this code to rewrite my php files to be html files, so technical.php would appear as technical.html
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
but it gives me a 404 error when visiting technical.html
Not Found
The requested URL /technical.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.
Did I get the order wrong in my htaccess?
Probably you'll have to specify the app folder as well:
RewriteRule ^(.*)\.html$ appFolder/$1.php
Adding an .htaccess rule in the subfolder the files were in fixed this! I thought one .htaccess in the root, controlled the entire site.

Redirecting all request to the root directory somewhere else?

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

Forms are broken in CodeIgniter

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.

Categories