Can any one help me in .htaccess.
My use case is, if there is index.php in the directory http://domain_name/user/ then load index.php else set rewriteRule to call forbidden.php.
I did a bit but not succeed yet. .htaccess code on my server is-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond ^(.*)$/index.php !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ forbidden.php?handle=$1
</IfModule>
I think there is an easier way to do what you want. There is no need for rewriting, simply define which page acts as the entry page. In the .htaccess file you can define:
DirectoryIndex index.php
ErrorDocument 404 /forbidden.html
Now, if anybody calls your directory http://www.example.com/user/, the page http://www.example.com/user/index.php will be shown.
If the file index.html does not exist, the server will return an error, so you can define an appropriate error page. With a leading /, you can define a single error page in the root directory, without the / it will look in the relative directory http://www.example.com/user/forbidden.php.
Related
So right now I have a index.php file that acts as an API in the root of a subdomain.
So if you go to http://some.domainname.com/index.php/items/ for example you get a response.
However if you go to http://some.domainname.com/items/ you get a 404 error.
I'm assuming there is some .htaccess rewrite command I can add to make this work but I haven't had any luck finding it.
Here is an example based on how Magento does it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It will effectively remove the index.php from your urls, but it won't stop anyone from accessing a file that actually exists, like www.example.com/i-exist.php
Note:
This is specifically geared toward a site whose urls alway end with a trailing slash (/), like www.example.com/index.php/somepage/. This is set with the RewriteBase / line.
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 want to load the draft.php file when i type the address in browser: localhost:81/mvcdemo .I have added this code in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ draft.php?url=$1 [QSA,L]
I have also changed Apache(httpd.config) file to get the rewrite on. So, what am i doing wrong?
You have conditions defined so that your RewriteRule will only be triggered, if the requested file, directory or link does not exist.
So if your .htaccess file is inside the directory mvcdemo and you're requesting said directory, the rule will not be triggered, because the directory exists.
Requesting http://localhost:81/mvcdemo/foo should trigger the rewrite rule, as it doesn't exist.
If you want all requests to the directory to be rewritten, then you can just do away with the conditions:
RewriteEngine On
RewriteRule ^(.*)$ draft.php?url=$1 [QSA,L]
Note that RewriteBase / sets the base URL as the document root, and since draft.php seems to be in the mvcdemo directory itself, you should do away with, as it will result in a draft.php not found error.
You could change apache's DirectoryIndex directive in your config file (or via an .htaccess) and append draft.php. Then it's auto-loaded like the usual index.html or index.php files.
/
.. .htaccess
.. index.php
I have two files shown above in my servers root directory. I want to write a rule in .htaccess so that every time it will pass every parameters (i mean (.*)) to index.php?controller=parameter and index.php will route it accordingly. How can I write such a rule. Thanks
This rewrites everything to index.php except for existing directories/files:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /pro/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
Passing url to controller is not mandatory, since you can use $_SERVER['REQUEST_URI'] (for example) in your index.php
I have a sitw built using codeigniter using htaccess. But on some requests are really strange. The include one specific file(trough codeigniter) at the begginning. This happens really often, if cache is tured off. It happens with different files and doens't matter if the file is on filesystem or request goes trough codeigniter. I guess there something up with htacccess, but i'm not sure.I'll include htaccess and screenshot from fiddler on the bottom:
<IfModule mod_rewrite.c>
RewriteEngine on
#Options -Indexes
#Force non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#If image, javascript or css file does not exists, then try application folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(images|js|css)/(.*)$ ./application/$1/$2 [L]
#If javascript or css file exists, send it to minifier
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)(\.(js|css))$ ./standalone/min/?f=$1$2 [L]
#If isn't file or folder then send it to codeigniter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 ./index.php
</IfModule>
And as you can see original request is to top-bar-large.png and echoing $_SERVER out from coudeigniter, shows "upper request" is to parim.js. And parim.js should javascript file after all. Any ideas?
PHP_SELF is index.php... so it seems like you are be getting a CI error page on which the js files are included.
By default, codeigniter comes with an .htaccess file in the application directory with Deny all; so anything in those folders will not be publicly accessible (including your images folder in there) unless you've modified it.
Also, make make sure the images exist.