I have this in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(gif|jpg|png|css|js|ico|flv|php|txt)$ index.php
Now i need to add another rule that will forward to index.html if no REQUEST_FILENAME is found.
So www.mysite.com would forward to index.html and www.mysite.com/file.html would forward to index.php.
I normaly use this rewrite rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
It rewrites all urls which dont exist to the index.php.
Just add another rule without the restrictions in the URL path ending:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(gif|jpg|png|css|js|ico|flv|php|txt)$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html
But it would probably be better to use the default error handling with:
ErrorDocument 404 /index.html
Otherwise your server won’t respond with a 404 on a request of a non existing file.
Related
I have following problem. My .htaccess looks like this
RewriteEngine On
RewriteBase /phpuserarea/
RewriteCond %{REQUEST_URI} !^mod/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
ErrorDocument 404 /phpuserarea/404/index.php
But whenever I try to directly access anything in the mod directory the server responds with 404.
Other problem is, that besides the index.php I want to allow direct access to let's say testa.php and testb.php
Those are my ajax files and I can't seem to find a solution.
Have it this way:
ErrorDocument 404 /phpuserarea/404/index.php
RewriteEngine On
RewriteBase /phpuserarea/
RewriteCond %{REQUEST_URI} !/mod/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
You need leading slash in RewriteCond %{REQUEST_URI} and better not use anchor ^ as your .htaccess is inside /phpuserarea/.
Also you don't need to use ?%{QUERY_STRING} in target since query string is automatically passed over to target if your rule doesn't overwrite it.
I've a main directory with a .htaccess file that looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The one in the subdirectory looks identical to this one. What I want the .htaccess files to do is redirect to the index file in each directory.
So if you visit main/example you get redirected to the index.php file in the main directory but if you visit main/subdirectory you get redirected to the index.php file in the subdirectory.
I've searched for an answer but haven't found anything that works.
Thankful for any help!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /$1/index.php [L]
This configuration do the following:
if we have this files
/index.php
/foo/index.php
/var/index.php
/var/boo/index.php
and you request the follow file
/foo/hello
you will go to:
/foo/index.php
and, if you request
/var/boo/world
you will go to:
/var/boo/index.php
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 -d
RewriteRule ^(.+)/ $1/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
You can use a simple solution like this one, no need to put .htaccess files everywhere
on your main directory you put one .htaccess file, you keep your code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
and you add those two lines
Options All -Indexes
ErrorDocument 403 http://domain.com/index.php
these two lines prevent the user to access the folder, and returns the statut 403 and then you redirect your 403 statut to the adress of your index.php of your main directory
I have wamp server installed and i can not rewrite my urls from a subdirectory
http://localhost/new1/articles/details.php?name=test
to
http://localhost/new1/articles/test
i tried like this
RewriteEngine On
RewriteRule ^new1/articles/(.*)/$ /new1/articles/details.php?name=$1 [L,R=301]
and this
RewriteCond /articles/ -d
RewriteRule ^/articles/([^/]*) /articles/details.php?name=$1
Nothing works...i get 404 not found.
what am i doing wrong?
Place this rule in /new1/.htaccess:
RewriteEngine On
RewriteBase /new1/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^articles/([^/]+)/?$ articles/details.php?name=$1 [L,QSA,NC]
These are the rewrite rules I'm using. At this point I'm trying to force bad urls to load index.php. The html tag <base href="/directory-name/" /> is in the top of index.php, so my css, images and other assets are loading properly.
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(.*)/(.*)/(.*) /index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^(.*)/(.*) /index.php?x=$1&y=$2 [L]
RewriteRule ^(.*) /index.php?x=$1 [L]
Url Examples:
www.domain.com/directory-name/
www.domain.com/directory-name/index.php
www.domain.com/directory-name/index.php/arg1/arg2/arg3
www.domain.com/directory-name/anything-here
www.domain.com/directory-name/anything-here/arg1/arg2/arg3
The first 3 url examples work fine and load index.php. But the 4th and 5th urls fail and dumps me back to the html root / where a xampp index exists. I'm assuming the failure of the last url rewrite is because the index.php is in a sub-directory off of root?
How would I fix that? How would I force the urls to load www.domain.com/directory-name/index.php even if something else is in the path?
Try these rules:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2 [L]
RewriteRule ^([^/]+)/?$ index.php?x=$1 [L]
I'm trying to write a rule for a directory of my site.
I tried something like this:
RewriteEngine on
RewriteRule ^(css|font|img|js)/([a-zA-Z0-9\/\.])$ $1/$2 [L]
RewriteRule ^([a-zA-Z0-9\/\_]*)$ index.php?url=$1 [L]
but I cant understand why it is not working well.
1- I want users have direct access to these directories css|font|img|js
(just for files, not directory lists. rewrite directory list to 403.php)
2- I want users have direct access to *.js|*.css files in view directory.
(not other file. rewrite other to 403.php)
3- I want to rewrite every other urls to index.php?url=$1
thanks in advance.
This part of your pattern: /([a-zA-Z0-9\/\.])$ means a slash, followed by exactly one character of a letter, number slash or a dot, then the end. I'm guessing you want something like:
ErrorDocument 403 /403.php
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(css|font|img|js)/ - [L,F]
RewriteCond %{REQUEST_URI} !\.(js|css)$
RewriteRule ^view/ - [L,F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
You can try these rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^(css|font|img|js)/ /403.php [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.(css|js)$ [NC]
RewriteRule !^view/ /403.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]
How about using a RewriteCond?
You could do something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(css|font|img|js)/
RewriteRule .* - [L]
RewriteRule ^([a-zA-Z0-9\/\_]*)$ index.php?url=$1 [L]
This way you do a noop rewrite for files inside the css, font, img and js directories and do the index.php rewrite for all other URLs.
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_URI} .php$ [OR]
RewriteRule ^(.*)$ target_file.php?=url$1 [L,NC]
#allow access to file js and css
RewriteCond %{REQUEST_URI} .(css|js)$
RewriteRule ^(.*)$ - [L,NC]
When use [L, NC] don't redirect to other page:
If you access localhost/page.php this don't redirect to target_file.php?url=/page.php
but continue in same url and appears the file target_file.php