I can't change my streaming website homepage - php

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.

Related

Redirect to a single file in apache

I have an index.php file in the DocumentRoot of a virtualHost in apache. It contains :
$path = $_SERVER['REQUEST_URI']
if($path == '/')
echo json_encode(some_data);
else if ($path == '/posts')
echo json_encode(another_data);
the thing is when I send a get request to / it respond with the data I wanted, but when requesting /posts or any endpoint other than /, the server respond with not found page.
How can I make it use index.php for every request that virtualhost ?
I want all the requests to be handled by index.php
WordPress deals with that by adding this to the .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Basically, you need to redirect all requests to index.php, but not using a 301 or 302, just rewriting. The RewriteCond lines are to make it so users can still access other files directly if they exist. You may not need those in your implementation.
Here's an explanation of the code:
RewriteEngine On turns on the rewrite engine :)
RewriteBase / Just makes sure we're starting from the document root
RewriteRule ^index\.php$ - [L] is a rule that does nothing if the request is for index.php. the [L] means no more rules will be processed.
The two RewriteCond lines make sure that there's not a matching file (1st line) or directory (2nd line) for the request. If there is, then the following RewriteRule won't take effect, and the server will serve up that file or directory. Otherwise:
RewriteRule ^index\.php$ - [L] will rewrite the request to /index.php, making your script handle the request without redirecting the user's browser to index.php.
If there is no .htaccess file, you'll need to add one, or add the rules to the conf file for this virtualhost. The conf file will also need to allow overrides to see the .htaccess file:
<Directory "/path/to/document/root">
AllowOverride All
</Directory>
You can use FallbackResource:
FallbackResource /index.php
Set it in your virtual host settings or in an .htaccess file.

API or template folder redirect in htaccess

I have a directory structure as follows:
.htaccess
api.php
template/index.html
When calling domain.com/api/someendpoint, the request is forwarded to api.php.
# enable apache rewrite engine
RewriteEngine on
# Deliver the folder or file directly if it exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-d
# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]
now, when calling domain.com, the request should be called to the index.html in the template directory:
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]
this does not work, the directoryListening is always displayed.
What is the correct way to get the index.html?
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]
The RewriteRule pattern (ie. .) unnecessarily matches everything except the document root. And the REQUEST_URI server variable always starts with a slash, so the condition will never match anyway. So, basically, the rule is doing nothing
To rewrite just the root then you just need a single rule in .htaccess:
RewriteRule ^$ template/index.html [L]
Ideally, this would go before your existing rewrites for your API, although it doesn't strictly matter since the other rewrites do not rewrite the root directory (because of the restrictive conditions).
# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]
Should everything really be sent to api.php, or perhaps just URLs that start /api/, as in your example? At the very least, you should be using the + quantifier, not *, since you want to exclude requests for the document root (currently, the 3rd condition is blocking requests to the document root from being rewritten).
Note that if your URLs are of the form /api/someendpoint and you are rewriting to /api.php then you need to ensure that MultiViews is disabled at the top of the .htaccess file:
Options -MultiViews

How to change the default site directory

I just setup a Wordpress in my server (www.albertotry.esy.es) and I added via FTP a new site files, but if I try to access the site, I don't see the index.html of the new site but the default Wordpress site.
I'm pretty sure there is a problem with the .htaccess file:
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
# 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]
</IfModule>
# END WordPress
What should I change to page that will display when I type "www.albertotry.esy.es"?
The .htaccess file you're using is the default WordPress one (except for that first line, which is a duplicate). It sends everything that is not a directory or file to index.php. If you want to load index.html when accessing the domain root only, then change the rule in question and add another after it:
Old Rule:
RewriteRule . /index.php [L]
New Rule:
RewriteRule !^$ /index.php [L]
The !^$ basically means "any string that is not empty" - in this case, the string is the request URI.
Then add a new rule below that one (you can also add it before, if you like - shouldn't make a difference):
RewriteRule ^$ /index.html [L]
Here, the ! is removed, which changes the meaning to "any request URI that is empty. As a result, requesting http://www.albertotry.esy.es/ will show the index.html file, but making any other request would rewrite to WordPress.
However, I believe that WordPress may check the content of the code between the comments # BEGIN and # END, so not sure if this is bulletproof. However, it does answer your question.

Cant get .htaccess to work proper

i am doing a URL-Shortener where i am trying to do it URL friendly.. Currently my adress is typed like this "example.com/index.php?name=sitename" i want it do be like "example.com/sitename".. I guess i could use some cool rewrite rule but i cant get it to work..
I did something like this but it wont work
Options All -Indexes
DirectoryIndex index.php index.html index.htm
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?name=$1 [L]
but i dont know :s?
Here is the code you need to place in DOCUMENT_ROOT/.htaccess
Options All +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php index.html index.htm
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?name=$1 [L,QSA]
Reference: Apache mod_rewrite Introduction
Your regular expression will fail on URLs with more than one / after the domain, because [^/] stops as soon as it sees one of these characters, and then it looks for the last slash with /$, which is not always the case.
Assuming you want to convert these URLs
example.com/HiThere
example.com/Your/Page
to:
example.com/index.php?name=HiThere
example.com/index.php?name=Your/Page
Try this rewrite rule instead:
RewriteRule ^(.*?)/$ /index.php?name=$1 [L]
This will look for all characters with (.*?) UNTIL it reaches the last /. I'm not 100% sure about the [L] flag, though.
the simplest method is to use 404 error to point to index.php and then get the sitename using php
.htaccess file
ErrorDocument 404 /index.php
PHP CODE
<?php
$sitename=substr(strrchr($_SERVER["REQUEST_URI"],"/"),"1");
now simply visit 'example.com/sitename' and sitename will be stored in $sitename.
And if($sitename==""){//Show your HomePage}
Hope it helps!!

How to redirect website to particular file?

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]

Categories