If I have /index.html and the user goes to domain.tld or domain.tld/index.html I want them to get that page. (Default behavior)
If I don't have /index.html but I do have /content.txt I'd like to serve the page /makeindex.php but not change the url.
If I don't have /index.html or /content.txt I'd like to serve /nocontent.php but not change the url.
So far I have:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /makeindex.php [L]
But I don't know how to limit this to domain.tld or domain.tld/index.html and I also don't know how I would create that two step process first checking for content.txt if I serve /makeindex.php or falling back to /nocontent.php if both those files don't exist.
You can use the following rule, put this at the top of your htaccess :
RewriteEngine on
#if /index.html exists serve it as a directory index and main page
RewriteCond %{DOCUMENT_ROOT}/index.html -f
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ /index.html [L]
#if content.txt exists , use makeindex.php as directory index
RewriteCond %{DOCUMENT_ROOT}/content.txt -f
RewriteRule ^$ /makeindex.php [L]
#else set /nocontent.php as directory index
RewriteRule ^$ /nocontent.php [L]
Related
I basically want to redirect all requests to index.php doesn't matter what, except those with certain REQUEST_URI. Those requests that look like image files, so have an ending like: .jpg or .png should be examined and if they are under the public/ folder (or any subfolders in any depth) and if they are they should be served and the rewriting process should stop here! If not, I want to redirect to a default image at public/errors/image-not-found.png and terminate rewriting process. The other exceptions are files that end with .js, .css, .html or .swf. They also should only be served if they are located under the public/ folder or any other subfolders. If not, a simple 404-Not found should be sent back. In either case of the last to the rewriting process need to stop of course.
Any other request should be redirected to index.php and appended as a query string. (even if the request points to a directory or to a file that is not under the conditions aforesaid, but exists, e.g: www.xyz.com/library/Database.php -> www.xyz.com/index.php?url=library/Database.php)
I have half-measure solution:This is how I redirect everything to index.php:
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I append a visual explanation of what I want. Maybe this is clearer:
Basically, you don't want to do anything if the requested file exists in public/ or any of its subfolders. So, first we deal with those:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public/.*\.(html|css|js|swf|jpe?g|png|gif|bmp|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
Now, that is over with. We now check whether an image was requested:
RewriteCond %{REQUEST_URI} \.(jpe?g|png|gif|bmp|ico)$ [NC]
RewriteRule ^ /public/errors/image-not-found.png [R,L]
Similarly for other static files:
RewriteCond %{REQUEST_URI} \.(html|css|js|swf)$ [NC]
RewriteRule ^ - [R=404,L]
Redirect everything now to index.php:
RewriteCond %{REQUEST_URI} !/index\.php$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^.*$ /index.php?url=$0 [R,L]
Following series of rules should probably mimic the flow-chart:
# for public folder pass through
RewriteRule ^public/.+?\.(?:jpe?g|ico|png|bmp|css|js|html|swf)$ - [L,NC]
# for other images
RewriteRule ^.+?\.(?:jpe?g|ico|png|bmp)$ /public/errors/img-not-found.jpg [L,NC,R=302]
# for other css|js|html|swf URIs
RewriteRule ^.+?\.(?:css|js|html|swf)$ - [L,NC,R=404]
# everything else, route to index.php
RewriteRule ^((?!index\.php).+)$ index.php?url=$1 [NC,QSA,L]
This is the architecture of my website :
/
app/
index.php
...
libs/
lib1/
file.php
lib2/
...
I need to access index.php by this url : domain.com/index.php
I tried this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)app
RewriteRule ^(.*)$ app/$1 [L]
It works, but inside my index.php, I call for example :
include('../libs/lib1/file.php');
It's doesn't work because this path refer to root now...
And I can't access to domain.com/libs anymore, because it's looking for domain.com/app/libs.
How can I do ?
The include() shouldn't care what the path the browser sees. That should be based on the local filesystem on the server. But your rules are affecting direct access to the libs, so try adding a few more conditions:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^app/
RewriteRule ^(.+)$ app/$1 [L]
RewriteRule ^$ app/index.php [L]
This makes it so requests for existing files or content won't get routed through the app folder.
I need to add RewriteRule which works only if there is a certain string in the URL, otherwise I need to load the content from another folder. If the URL contains the string test, I need to send it to index.php as a parameter, else the content should be loaded from the directory folder.
For example: The root folder of the project is new_project. If the URL is http://localhost/new_project/test/something/, then I need to send test/something/ to index.php as parameter. Else if the URL is something like http://localhost/new_project/something/, then I need to load the content from directory/something folder.
Following is the .htaccess file I've written so far:
Options +FollowSymLinks
RewriteEngine on
# Force Trailing Slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# Serve other requests from the directory/ folder
RewriteRule ^(.*)$ directory/$1 [L]
What needs to be changed in the above .htaccess file so that the occurrence of test string in the URL passes the string after the string test along with the string test to index.php and if the URL doesn't contain the string test then loads the content from the directory folder?
You can test for the test/ inside the RewriteRule itself. Place the more specific rewrite rule before the "catch-all" directory/ rewrite rule.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# All requests starting with `test/` go to index (without the `test` part).
RewriteRule ^test/(.*)$ index.php?/$1 [L] # Not sure how GET parameters starting with `?/` behave.
# All others go to directory. Assuming not a valid file or dir.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ directory/$1 [L]
OK, I'm pants at rewrite rules in .htaccess files!
My desired scenario is (using the URL http://doma.in/ as an example):
First check to see if an index.html file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php
To expand on my example, say we requested the URL http://doma.in/js/foobar.js:
First check to see if an foobar.js file exists in the /public/js sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=js%2Ffoobar.js
That would cover static files but I also need URLs like http://doma.in/foo:
First check to see if an foo file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=foo
And a URL http://doma.in/foo/bar:
We can assume the file foo/bar does not exists in the /public sub-dir as files can't be named like that.
So serve (rewrite to) index.php?controller=foo&action=bar
I'm sure if this complicated (for me) bit is covered then I can work query-strings into the occasion too; so http://doma.in/foo/bar/foo/bar would serve index.php?controller=foo&action=bar&querystring=foo%2Fbar.
I'd also like to make sure that a trailing slash is handled the same as if a trailing slash was omitted, for example: http://doma.in/foo/bar/foo/bar and http://doma.in/foo/bar/foo/bar/
I'll handle 404s from within my app as if the file did not exist, it would redirect to index.php which does exist - I'm happy with this unless you've a better solution :)
I really hope all this makes sense as I've been looking to find a solution to this scenario all day now and this is all I have:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /prompt-v3/
#RewriteCond %{REQUEST_URI} ^/prompt-v3/(.*)$
RewriteCond $1 !^public
RewriteRule ^(.*)$ public/$1 [R]
The commented-out lines deal with a sub-dir when on a remote host. So far I can redirect to the /public sub-dir if the file exists there and that's about it.
Thank you everyone for your help!
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /sub-dir/
#RewriteCond %{REQUEST_URI} ^/sub-dir/(.*)$
RewriteRule ^index.html$ $1 [L,R=301]
RewriteCond $1 !^public
RewriteCond $1 !^lib
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond $1 ^public/$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ lib/bootstrap.php [L]
RewriteCond $1 !^public/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^public/(.*)$
RewriteRule ^(.*)$ lib/bootstrap.php?path=%1 [L]
This will look for an index.html file in the public directory and if it does not exist rewrite the URL to lib/bootstrap.php. it in fact checks for any request as a static file in the public directory first and deals with canonicalisation too.
Put this .htaccess in your document root
<ifModule mod_rewrite.c>
RewriteEngine on
# For /js/foobar.js (/js/*)
RewriteRule ^js/(.*)$ /public/js/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)$ index.php?controller=$1 [NC,L]
# For foo/bar (/*/*?*)
RewriteRule ^(.*)/(.*)$ /public/$1/$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)/(.*)$ index.php?controller=$1&action=$2&querystring=%{QUERY_STRING} [NC,L]
</IfModule>
I have 2 questions.
I am currently using wamp server to serve my website.
The homepage is 'localhost/prefix/index.php'
Question 1:
a. I would like it so my home page is:
'localhost/prefix/'
instead of
'localhost/prefix/index.php
b. I would like it so:
'localhost/prefix/admin/profile.php'
is
'localhost/prefix/admin/profile'
How do I go about doing this (I have googled and I am very confused by the syntax)?
Question 2
If I have a url like
'localhost/prefix/games?title=hi'
how can I make it so the url is like this:
'localhost/prefix/games/hi'
Thanks in advance!!
I really have got lost.
EDITED::///
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R]
Is what I have so far.. It does nothing... But everyone says it should! (the htaccess file is doing something because if I do something random, it throws up errors).
EDITED::///
This seems to remove .php and index.php from the url:
RewriteEngine On
RewriteBase /prefix/
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Problem now is that my prefix base is not working so it ends up going to
localhost/something/something
rather than
localhost/prefix/something/something
Any ideas?
EDITED::///
I have sussed out that the above code actually works perfectly if the page i'm directing to is in a sub folder. so for example.. this will work:
localhost/prefix/admin/dashboard
But this (because the file is in the root directory, doesn't)
localhost/prefix/login.php
it redirects me to
localhost/login
Any ideas?
EDIT::///
If you are having problems getting it to work. close your browser down and restart... I had caching issues.
This code above will remove .php and also remove index.php.