How to redirect website to particular file? - php

There is a domain name. Let's say domain.com.
When the user types domain.com only, he should be redirected to the address domain.com/file.html. How can I do that with a .htaccess file?
Plus, what does RewriteRule ^index\.php$ - [L] mean? Would it help me?

Add to your .htaccess file
DirectoryIndex file.html

Check out this site: .htaccess tricks and tips It's a good reference for rewrite rules.
As for what does RewriteRule ^index.php$ - [L] mean. It's going to ignore anything with an index.php ending Rewrite Rule meaning

To redirect you can make a index page (the first page that the user visits)
DirectoryIndex file.html
modifying your .htaccess file according to this link.
For RewriteRule you need to able the mod_rewrite module in apache and then make in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) file.html [L]
</IfModule>
In the RewriteRule you can put regular expressions to identify the url and redirect the user to a file that you want (in this case you would redirect all your links to file.html). More info here.
Moreover in the configuration file of the apache server by default is possible you have the next configuration:
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.php index.php4 index.php3 index.cgi index.pl index.html index.htm index.shtml index.phtml
</IfModule>
So, by default if you have a file called index.php in your webroot of your domain.com always that file would be called first.

Try putting the following in your .htaccess file located in the root of domain.com
RewriteEngine On
RewriteBase /
#put all the following rules before any other rules in the .htaccess file
RewriteRule ^file\.html$ - [L]
#domain.com or any subdomain
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
#the root of domain
RewriteCond %{REQUEST_URI} ^/?$ [NC]
#return file.html
RewriteRule . file.html [L]

Related

I can't change my streaming website homepage

I'm trying to change the homepage of my site but nothing is happening. When entering the code below "DirectoryIndex index.html #to make index.html as index" my home page remains the same. I'm making changes to the file in .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^index.php - [L]
RewriteRule ^index.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ index.php [QSA,L]
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
DirectoryIndex index.html #to make index.html as index
My website is written in php and I want to put the homepage in html
RewriteRule ^(.*)$ index.php [QSA,L]
Change the * quantifier to +, so that it only matches non-empty URL-paths, to allow the DirectoryIndex document (ie. index.html) to be served for requests to the homepage, instead of passing the request through to the front-controller (ie. index.php). Or, simply use a dot (.) as the regex since you aren't doing anything with the captured URL-path. For example:
RewriteRule . index.php [L]
(The QSA flag is not required here.)
Although, since you are using a front-controller pattern (ie. routing all requests to index.php), you should probably be configuring the appropriate response to be served from index.php instead?
Aside:
DirectoryIndex index.html #to make index.html as index
You should remove, what you think is a comment at the end of the line, ie. "#to make index.html as index". That's not actually a comment. Apache does not support line-end comments (only full-line comments are supported). In this instance, #to, make, index.html, as and index will be seen as additional arguments to the DirectoryIndex directive (so you don't actually get an error).
See my answer to the following question regarding the use of line-end comments:
Error 500: .htaccess RewriteCond: bad flag delimiters
UPDATE:
Alternative solution
Try the following instead (this replaces the entire .htaccess file above):
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^index\.(php|html)$ - [L]
# Rewrite any request for anything that is not a file to "index.php"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
# Rewrite the homepage only to "index.html"
RewriteRule ^$ index.html [L]
The <IfModule> wrapper is not required. (This is your server so you know if mod_rewrite is enabled or not and these directives are not optional.)
Make sure you've cleared your browser cache before testing.

Rewrite root directory to subdirectory

I am developing a website that runs a backend and a frontend out of two different directories.
/frontend/
/admin/
I would like to run the /frontend/ as the root whilst still running the /admin/ as it's own directory.
What do I need to write into the .htaccess to do this.
This is how my .htaccess file looks currently:
# Set the default handler.
DirectoryIndex index.php
# Prevent directory listing
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 404 /admin/404.php
</IfModule>
Any and all answers will be appreciated.
Add this after RewriteEngine On:
# Redirect any requests within frontend/, removing frontend/
RewriteRule ^frontend/(.*)$ http://www.example.com/$1 [R=301,L]
# Rewrite anything that is not in admin/ to frontend/
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^(.*)$ frontend/$1 [L]
Be sure to update the base website URL on line 2.

server or domain defaulting back to index.html when index.php is entered

Like others I want to be able to run both index.html and index.php on a site. I want the default site.com to go to site.com/index.html and when typed into the address bar "site.com/index.php" I want this to go to "site.com/index.php" . I have the same setup on another Wordpress site that works fine but I can't replicate it on a new one.
It will default to "site.com/index.html" stripping out the "index.html" in the address bar, but if I type site.com/index.php in the address bar it reverts to site.com . It strips off the '/index.php' at the same time. Here is my htaccess file. Anything obvious? thanks~
DirectoryIndex index.html index.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
The way WordPress works is that it tries to prevent duplicate URLs, thus, when you go to example.com/index.php it will (301) redirect you to example.com/.
I gues you could still have index.php and index.html by using the right order in your .htaccess (taking into consideration te redirect I mentioned):
DirectoryIndex index.php index.html
Then you will have:
index.php ===> /
index.html ==> /index.html

Use htaccess to redirect root to subfolder without showing it in URL

I am currently hosting a website using the Silex framework on a shared server and I have a problem...
Silex is like Symfony, there is an app.php file located in a /web/ subfolder : the website is then only accessible via the URL website.com/web/. I cannot create a virtual host as it is a shared server, so I think the solution is to use an .htaccess file...
I managed to redirect website.com to website.com/web/ automatically but I don't really like this option. I would rather website.com pointed directly to website.com/web/ but I don't know how to do this by just using a .htaccess file. I have been trying to solve this problem for hours now and it's killing me...
At the moment I use this file :
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/app.php [QSA,L]
</IfModule>
but it just redirects you from website.com to website.com/web
Is there anyway I can make the root url directly point to the /web folder with a .htaccess file?
Thank you very much :)
If you want access to http://example.com/subdirectory just by typing http://example.com this should work.
# .htaccess main domain to subdirectory redirect
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/ [L]
Try this htaccess configuration:
DirectoryIndex web/app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/web/
RewriteRule (.*) /web/$1
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /web/app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>

.htaccess - redirect a php File to an Virtual Directory

What are the .htaccess lines to make a Virtual Directory from a PHP File?
www.domain.de/file.php should go to www.domain.de/file/
Thats it:
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^file/ file.php [L]
Options -Indexes
Something like this:
RewriteEngine On
RewriteRule ^(.+)\.php$ /$1/ [L]
This will make it is if you enter http://www.domain.de/something/, the webserver will rewrite the URI to /something.php. You just need to make sure you serve your links as /something/

Categories