My .htaccess file contains the following directives
DirectoryIndex index.html index.php
# redirect invalid requests and missing files to the home page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/ [L]
The problem is that many programmers(loosely used term) worked on this site. Some directories use index.html and some use index.php.
If a directory uses index.php, the request to www.mydomain.com/directory looks for www.mydomain.com/directory/index.html and is redirected to www.mydomain.com before it can look for www.mydomain.com/directory/index.php
How can I both try all DirectoryIndex files AND redirect missing files to the home page?
How can I both try all DirectoryIndex files AND redirect missing files
to the home page?
I don't think that's possible with mod_rewrite or mod_alias modules. You have to look for other options to solve the problem. Here is an idea using the same DirectoryIndex directive to force the loading of a file at root directory as the last option:
Place this line in one .htaccess file at root directory:
DirectoryIndex index.php index.html /missing.php
Create missing.php in root directory with these lines of code:
<?php
header("Location: http://www.example.com/"); // Redirect
?>
No additional directives or rules are needed. missing.php at root directory will be loaded if none of the previous files (From left to right) is found at the target directory. All requests must have a trailing slash for this to work, though.
missing,php is just an example. Any file name can be used.
UPDATED with additional option:
According to OP comment:
"But how do I handle missing files such as example.com/file-not-on-server.php"
In this case, mod_rewrite is indeed a solution.
You may try this in the .htaccess file at root:
# Directive that solves the original question
DirectoryIndex index.php index.html /missing.php
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Next condition is met when the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
# If previous condition was met, use the next rule
RewriteRule ^(.*)$ /index.php [L,NC]
NOTES:
1. /index.php in the rule, is just an example. It can be any file.
2. For permanent redirection, replace [L,NC] with [R=301,L,NC].
Related
I have a root directory which has only test index.php file and .htaccess. Can you please help with forwarding root request (example.com) to child directory (example.com/wp/index.php). I don't need any redirects, because they will change the URL in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule $^ /wp/index.php [L]
</IfModule>
It is my .htaccess file in root directory for now. And with that .htaccess, server just return me /index.php (root index, not from sub directory).
So I don't think that it is very hard question, but I can't find where I'm making mistakes.
RewriteRule $^ /wp/index.php [L]
You have the regex anchors (end of string $ and start of string ^) round the wrong way. However, you could also have issues if you have a DirectoryIndex set (mod_dir will trigger an internal subrequest for /index.php, so the URL-path is not necessarily empty).
Try something like the following instead:
RewriteEngine On
RewriteRule ^(index\.php)?$ /wp/index.php [L]
The RewriteRule pattern ^(index\.php)?$ matches either an empty URL-path, or index.php (the result of the mod_dir subrequest).
No need for the <IfModule> container and RewriteBase is not being used here.
On root of my server, I have codeigniter with htaccess. I have created a wp/ folder along codeigniter's sub-folders (e.g. application, config) in which I have placed all the wordpress files. Now when someone types
www.abc.com
i redirect him to
www.abc.com/wp/
but I want to hide wp in the url. How can I achieve this without modifying my current file structure? I want wordpress to be my site's front and codeigniter to be my site's backend. The file structure is shown below:
In your root directory, create a .htaccess with following codes. This will map all your http requests to the /wp/ directory.
# ==>Initializaion…
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# ==>Without this line, your /wp/ directory may become visible sometimes
DirectorySlash Off
# ==>Map everything to your /wp/ directory
RewriteRule (.*) /wp/$1 [L]
You also want to hide the /wp/ directoy from URL. For this, create another .htaccess in your /wp/ directory and write following lines:
# ==>Initializaion…
Options +FollowSymLinks
RewriteBase /
# ==>Set default file
DirectoryIndex index.php
#==> If the requested resource is NOT a file, or the URL contains “/wp/” anywhere in it,
# then serve the default file instead
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{THE_REQUEST} /wp/
RewriteRule .* index.php [L]
If someone types “/wp/” anywhere in the address bar, they will not see what is in it. Instead, they will see your homepage.
You may have to adjust the code to suite your requirement.
I want to know how to get default index.php when there is no trailing slash after folder.
For example -
My website is - http://xx.xx.xx.xx/abc/xyz
When I put this url in browser - browser automatically add slash to xyz/ and takes index.php page under 'xyz' directory but when I use this url with curl then it won't work.
What's the solution for this problem?
P.S. - http://xx.xx.xx.xx/abc/xyz is sample url i am using here but my url structure is same like this
Here is the solution I found - I created .htaccess file with below settings and it works :)
DirectorySlash Off
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?!^.+?/$)^(.+)$ /$1/index.php [L]
You need DirectoryIndex index.php at top of your .htaccess:
DirectoryIndex index.php
DirectorySlash On
In my site root directory. there are donghua.php and index.php. now, is there a way to use .htaccess file to let the visitor access my site .the default shows example.com/donghua.php. not example.com/index.php. thank you. the server is Apache.
ps:The user still can access example.com/index.php
i using DirectoryIndex donghua.php in the .htaccess. the default page is ok. but when i access example.com/index.php it redirect to example.com/donghua.php. if i forbid it redirect how do i do?
If you have access to the virtual host config file (or you should even be able to do this in htaccess) you can set the DirectoryIndex to be donghua.php if you want, i.e. instead of something like this:
DirectoryIndex index.html index.php default.html
Just do:
DirectoryIndex donghua.php
So whenever someone goes to your website, example.com, your apache installation will read donghua.php as the default index file.
Change your redirection rule in .htaccess file for index.php. If you open the .htaccess file, you can find the follwing lines,
RewriteCond %{THE_REQUEST} ^.*/example.php
RewriteRule ^(.*)example.php$ http://www.test.com/$1 [R=301,L]
Change or remove this rule for index.php. Better remove or comment it then it won't redirect to another page
"RewriteCond" this code check the codition if the browser requesting the file name index.php. If the condition is satisfy, then the next line will execute that is "RewriteRule". This "RewriteRule" will redirect your URL.
Search and find the name "index.php" in .htaccess file. comment out that lines. syntax to comment is "#".
Example:
#RewriteCond
#RewriteRule
Take back up before changing anything in .htaccess file.
To make your URL lovely and suitable for search engine ranking.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]
To redirect
redirect 301 /old-page.php http://www.yoursite.com/new-page.php
In order to convert my dynamic URL i.e www.3idiots.co.in/index.php to static url i.e www.3idiots.co.in/index.html, I edited my .htccess file and put the following code in it:
RewriteEngine On
RewriteRule ^index.php$ /index.html [R]
when i uploaded this file in the root directory,and try to open the page, I got the error
404 page not found error, www.3idiots.co.in/index.html not found.
You have to actually have a file named index.html. Right now you don't. The rewriting/redirecting is working fine, you're just redirecting to a non-existent page/file.
I'm a little confused as to what you're actually trying to do. If you just want to move index.php to index.html, rename the file. Rewriting makes it so that if someone tries to open index.php they will be redirected to index.html, but you still have to have an index.html file for them to be redirected to.
RewriteEngine On
# Send the user to index.html
RewriteRule ^index.php$ /index.html [R]
# Tell the server that index.html really means index.php
RewriteRule ^index.html$ /index.php
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php$ /index.html [L,R=301]
RewriteRule ^index\.html$ index.php [L]
The first rule redirects every direct request of /index.php externally to /index.html. And the second rule rewrites requests of /index.html internally to /index.php.
Are you sure mod rewrite is enabled & working?
1) create an html page called found.html with whatever you want in it, but some text to be sure it's loaded (not a blank page basically) and put this in an file called ".htaccess" :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ /found.html [L]
2) upload both your .htaccess and the found.html files in your domain's root
3) Just try to load -www.example.com/find.html (with your real domain of course). If mod_rewrite is available, you should see the content of found.html while browsing find.html (which does not physically exist by the way).
If it does not work, try :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ found.html [L]
In the Apache Conf file, you also need to make sure that AllowOverride is set to a value that will allow .htaccess to be processed.
Normally it's AllowOverride all
RewriteEngine On
RewriteRule ^index.html$ index.php
RewriteRule ^$index.htm/$ index.php [L]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index.html$ index.php
Try this code...
It will work for your problem /..
Best Of LUck
If it solve your problem..
Visit my site