I have searched all day for a solution to the problem I'm having and found many proposed solutions, although none of them worked. I have url rewriting set up and everything works fine except for one folder. If a folder or file exists do not rewrite it otherwise send it into index.php for handling. The one subfolder that is giving the problem is password protected. The password protected folder gets re-written into the index.php as if the subfolder is invalid. The other subfolders work fine and do not have a .htaccess in them. folder structure and .htaccess below.
Folder structure with relevant files
public_html(root)
fonts
images
js
css
email
.htaccess
index.php
.htaccess
index.php
public_html/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L]
public_html/email/.htaccess
AuthType Basic
AuthName "email"
AuthUserFile "/home/site/.htpasswds/public_html/email/passwd"
require valid-user
I tried to turn off rewrite inside of email folder to prevent the rewrite form propagating in this solution had no apparent effect.
## turn off rewrite engine
RewriteEngine off
AuthType Basic
AuthName "email"
AuthUserFile "/home/site/.htpasswds/public_html/email/passwd"
require valid-user
I also tried filtering out the folder in the root .htaccess which also seemed to be ignored as well and made no apparent difference.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^email
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Any help with this would be greatly appreciated!
update 2/23/2015: 11:20am(est)
One thing I have noticed if I disable the url rewriting and authenticate for the folder then re-enable rewriting everything works as expected. It appears to only happen if you are not already authenticated.
After giving up on this for a while I looked back into this today and found the solution. Posted below. The 401 error is interpreted by apache as a 404 error and passing along to the rewrite. The solution is to add ErrorDocument 401 default before the RewriteEngine is turned on. This seems redundant telling it to use the default error message explicitly, but it does resolve the error.
ErrorDocument 401 default
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^email [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Your filtering out email folder wasn't working because your condition isn't being matched. You almost had it. REQUEST_URI also matches the prepending / so because you didn't have it before email it was not matching. You have to include it when using REQUEST_URI in the condition. It's a real folder so you shouldn't need it but try it this way.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/email [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also clear you browser cache before trying updated rules.
Related
I have index.php that reads full path by $_SERVER[‘REQUEST_URI’] variable. My task is when user enter: www.domain/resource/777 redirect to index.php with path /resource/777 and parse $_SERVER[‘REQUEST_URI’] to do some logic. But I have also real files and folders like:
css/theme.css
assets/assets.js
resource/locale.js
When I try this config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
Client could not get all css and etc files. How to write correct rule in Apache to achive this?
If you are using Apache 2.2.16 or later, you can replace rewrite rules entirely with one single directive:
FallbackResource /index.php
See https://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource
Basically, if a request were to cause an error 404, the fallback resource uri will be used to handle the request instead, correctly populating the $_SERVER[‘REQUEST_URI’] variable.
Your rule is fine. Issue with css/js/image is that you're using relative path to include them.
You can add this just below <head> section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Also keep just this rule in your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]+$ index.php [L]
According to the documentation REQUEST_FILENAME only evaluates to the full local filesystem path to the file if the path has already been determined by the server.
In a nutshell this means the condition will work if it is inside an .htaccess file or within a <Directory> section in your httpd.conf but not if it's just inside a <VirtualHost>. In this latter case, you can simply wrap it in a <Directory> tag to get it working.
<VirtualHost *:80>
# ... some other config ...
<Directory /path/to/your/site>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
</Directory>
</VirtualHost>
You can set the error document in your htaccess file so that index.php will be used then a non existing file is requested:
ErrorDocument 404 /index.php
For now only this helped to me:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/src/
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/resource/
RewriteCond %{REQUEST_URI} !^/data.json
RewriteRule ^(.*)$ /index.php [L]
I have tested the following pattern in online testers and everyone of them the has it working fine. In addition the rules are working for other similar iterations no problem.
RewriteCond %{REQUEST_URI} !404 [NC]
RewriteRule ^manager/services/([\w\-]+) manager/services/controller.php?PAGE=$1 [QSA,L,R=302]
For the REQUEST URI (cant give the URL atm as its still insecure):
/manager/services/get-items
But this rule is throwing a 404 error. controller most definitely exists and I can visit it manually with no problem..
The only other possibility I Can think of is some kind of interaction with the directory level htaccess which only as allow,deny for my ip address setup but no redirects.
Put this in the htaccess in the root and give this a try.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^manager/services/([^/]+)/? /manager/services/controller.php?PAGE=$1 [QSA,L]
I'm assuming you have htaccess enabled since you said you had other stuff in there but make sure it's set to AllowOverride All in Apache config.
Edit:
Or you can try and add the code to the manager folder htaccess file with this.
RewriteEngine On
RewriteBase /manager/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^services/([^/]+)/? services/controller.php?PAGE=$1 [QSA,L]
For posterity I had a directory named 'services' in the web root on a GoDaddy server. Something is mucking it up and causing a mix of 404 and 500 errors for anything related to that folder.
Renaming the folder to web_services resolved the issue.
This is my current .htaccess file:
DirectoryIndex requestHandler.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ requestHandler.php?/$0 [L,QSA]
I want to redirect alle requests to "requestHandler.php". It works now, but it is also possible to access sites with the direct link, and I don't want that.
For example:
It now works with ".../api/register" , but you can also access it but going to ".../register.php" and that shouldn't be possible. It should only be possible to go to register.php by ".../api/register".
I think I had it working before, but as I continued editing I've seemed to mess it up.
requestHandler.php should be working properly and when I enter ".../register.php" it is not at all redirected to requestHandler, but if I enter ".../registe.php" or ".../register.ph" it is redirected there.
Any solutions?
The rule you posted is only checking if the file/directory exists, and if it doesn't, then redirect to requestHandler.php. So naturally, if an existing file is entered in the URL, it will not redirect.
If you want all ".php" files to get redirected as well, you'll need a more specific Rewrite rules. Something like this maybe:
DirectoryIndex requestHandler.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ requestHandler.php?/$0 [L,QSA]
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^(.*)$ requestHandler.php?/$0 [L,QSA]
You can probably consolidate that into one rule block, but at least this way it's very readable. The second rule block is only checked if the requested file is not a file or a directory.
I have a website with this in the .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?menu=$1 [L,QSA]
#DirectoryIndex index.php
My problem is even, if I change a single letter the website is rendering a 500 Error. Even if I empty complete file, it still shows me the 500 Error.
What I want to do is, there is a page like this on the domain
http://www.example.co.uk/brochure/generate.php
The file generate.php does exist in /brochure/ directory.
But still the generate.php does not load and it is loading the index.php file.
Any help?
Try the following which I know should work:
<IfModule mod_rewrite.c>
#Turn the Rewrite Engine ON
RewriteEngine On
#Set the base where to rewrite the requests
RewriteBase /
#Allow direct file access
RewriteCond %{REQUEST_FILENAME} !-f
#Allow direct directory access
RewriteCond %{REQUEST_FILENAME} !-d
#Disallow certain files.
RewriteCond %{REQUEST_FILENAME} !brochure\/generate\.php$
#Rewrite URLS To GET[menu] and make sure this is the last rule.
RewriteRule ^(.*)$ index.php?menu=$1 [L]
</IfModule>
if you do not see any rewriting taking palce then check to see the module for rewrite is installed.
If an empty file triggers a 500 status code as well, then the error is somewhere else: you are not editing the real .htaccess file being used by Apache, the error comes from the application, etc.
In any case, you should find the error log: that's where the exact details are shown. In Linux, it's normally under /var/log/httpd. In Windows, it can be under %ProgramFiles%\Apache Software Foundation\Apache2.2\logs. If it's a shared hosting account, you'll probably have an "Errors" or "Logs" feature in your control panel.
At the root of my site... www.domain.com . want to add some static pages that the page url can be set from the user.
So if the users set as url profile then full page url should be www.domain.com/profile ..
So far a simple rewrite rule would do the job.
trasnlate it to something like /staticpage.php?tag=profile
The problem that i want some pages like www.domain.com/shop at the root which arent static...
So what can i do if all the requests for the main directory go to /staticpage.php?tag=$1 ?
I recommend using mod rewrite to send everything to your index.php file and using a front controller to do this. It makes it much easier.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
You'll find a lot more help about mod_rewrite on ServerFault as a general rule, but I tend to do this:
RewriteEngine on
RewriteRule ^static.*$ - [L]
RewriteRule ^assets.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule .* /router.php
where "static" are uploaded files, and "assets" are production graphics/stylesheets/js libraries etc.