In an application I want all users to start at index.php. So I want to redirect all direct URLs to other pages to this file.
When I use the following syntax, only the non-existing files are rewritten, but I want to redirect the existing URLs too.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f means: if this file does not exist, apply the RewriteRule.
RewriteCond %{REQUEST_FILENAME} !-d means: if this directory does not exist, apply the RewriteRule.
Use:
RewriteEngine on
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
EDIT:
If you want to redirect only when the URL does not start with /index.php yet, use this extra condition:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php/?path=$1 [NC,L,QSA]
Related
I created the following .htaccess file and I am wondering if it could be improved:
RewriteEngine On
RewriteRule ^site$ ./site/ [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
I want to redirect visitors which enter www.domain.com/site/ to www.domain.com/main/ and all other requests to www.domain.com/404.php?url=... This works, except when the user doesn't enter a trailing slash (www.domain.com/site).
You should be able to do that with a /? like
RewriteEngine On
RewriteRule ^site$ ./site/? [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
For the first rule, you rewrite site to site/ and then rewrite site/ to main/. I would rather do it in one step, e.g.
RewriteRule ^site/?$ ./main/ [L,NC]
The second rule would stay as it is.
With the current rules, you only redirect nonexisting paths to 404.php. If this is what you want, everything is fine. If not, you should remove !-f and !-d conditions and add a condition to exclude main from being rewritten to 404
RewriteCond %{REQUEST_URI} !/main/ [NC]
Even though the substitution ./404.php is valid, I would write it as
RewriteRule ^(.*)$ 404.php?url=$1 [L,NC]
I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ api.php?url=$1 [L]
Above shows my current configuration.
The problem with this is it only seems to be redirecting if a directory or file exists after the folder the htaccess file is in.
How can I rewrite this so it literally just redirects every single request to api.php with the full requested path? I'm not looking to see whether directories or files exist in the htaccess file, I can do that in api.php, I just want to push all the requests through that file.
You can use:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^((?!api\.php$).*)$ api.php?url=$1 [L,QSA,NC]
Lookahead (?!api\.php$) means rewrite everything except /api.php to /api.php with query parameter url.
like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /api.php?path=$1 [NC,L,QSA]
make sure you have this before the route
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]
I am working on an .htaccess-file lying in mysite.com/dir/.htaccess that redirects mysite.com/dir/page to mysite.com/dir/page.php if possible and redirect to mysite.com/dir/ if not possible. So any "wrong" request will be redirected to the main page. The code im using is:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [R]
This all works well. (By the way: i want to determine the base-dir of the file, because i maybe want to change the directory name later, but not change the file). What i now want is to block some directories for the user, because they contain php-libraries that should not be accessible. how can i e.g. block the directories mysite.com/dir/lib/* and mysite.com/dir/lib2/* and redirect them to mysite.com/dir/ again like i did before? I also want to use code not like
RewriteRule ^lib/(.)* %{ENV:DIR_BASE}
that would redirect mysite.com/dir/lib/../page to the main page instead of mysite.com/dir/page where it should belong. i tried very much with %{REQUEST_FILENAME} and %{REQUEST_URI}, but i am only a beginner when it comes to mod_rewrite. do you know a solution?
This should work:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
RewriteRule ^lib2?(/|$) %{ENV:DIR_BASE} [L,NC,R]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.*)$ $1.php [L]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]