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]
Related
On my sites, when type www.abc.com/folder, it's redirect to index.php. And I want the script don't touch on others folder and files. So is there anyway to prevent this?
This is the .htaccess file:
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
To redirect everything from root only to home page you can use this rule:
RewriteEngine On
RewriteRule ^[^/]+/?$ / [L,R]
RewriteCond %{request_filename} -f
RewriteCond %{request_filename} -d
RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]
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
I have a htaccess file that I am try to make a rewrite but it causes me multiple problems. First here is my htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2&%{QUERY_STRING}
RewriteRule ^login$ login.php?category=$1&%{QUERY_STRING} [L]
The first problem is that when I make a request like "mysite/foo/bar/", it does rewrite the query string in category and itemId but it also changes the base directory for my css files and Js files to /foo/bar/design/css/style.css
The second problem is that I have a few path like "login/","register/","users/", and when I try to load the corresponding file, it says:
The requested URL /login/ was not found on this server.
Thanks for the help!
First change your .htaccess like this:
RewriteEngine On
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2 [L,QSA]
RewriteRule ^login/?$ login.php?category=$1&%{QUERY_STRING} [L,QSA]
How can I use .htaccess to rewrite URL for certain pages only?
For example, I want it to work on index.php but not the rest of the pages on the site or on all pages but index.php.
I run in to issues with sub domains and php scripts where the URL redirecting that I am using will mess up stuff. Below is an example of the kind of script I want to use.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php/?node=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php/?node=$1 [L,QSA]
Can anyone point me in the right direction?
You can pass URL as REQUEST_URI server variable:
RewriteCond %{REQUEST_URI} ^(your_url)$
RewriteRule %1 do_some_stuff
As an example:
RewriteCond %{REQUEST_URI} ^index.php$
RewriteRule %1 - [F]
Or just pass it in RewriteRule:
RewriteRule ^index.php$ do_some_stuff
You should check out RewriteCond (conditions) for .htaccess
Check http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ for a lot of information and examples.
You could also write a rule just for index.php and then flag it as the last rule [l]
RewriteRule ^/index.php(.*) /index.php$1 [L]
RedirectRule ^/(.*)$ /index.php?path=$1 [L]
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.