Accessing a websites home file without the file name and extension - apache

I am working on a website that has a URL such as the following:
http://www.domain.com/directory/home.php
I was wondering if it is possible to set up the site so users can access it like so:
http://www.domain.com/directory/
And the browser will automatically find and render the home.php file?
Many thanks in advance!

Assuming you are using Apache, from the docs:
The DirectoryIndex directive sets the list of resources to look for,
when the client requests an index of the directory by specifying a /
at the end of the directory name. Local-url is the (%-encoded) URL of
a document on the server relative to the requested directory; it is
usually the name of a file in the directory. Several URLs may be
given, in which case the server will return the first one that it
finds. If none of the resources exist and the Indexes option is set,
the server will generate its own listing of the directory.
Add this to the httpd.conf file :
DirectoryIndex home.php
Then to remove the .php from the URLs, add this to the .htaccess file in the root folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

Related

How to prevent parent directory listing inside root folder when queried in the URL with APACHE server

In the .htaccess file when we type
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
If there is a directory that exists inside the root folder for example say errors. It will be shown if someone enters the URL. What can we do to prevent the opening of the parent directory structure directory in such a case? How to prevent exposing parent directory structure to the user in case of all the folders that exist in the root directory and queried inside the URL
like url= localhost/errors
Any command for that. I am using apache 2.4.46
To prevent directory listing you can use Options directive
Options -Indexes
This will show a 403 forbidden error if you try to access a directory on your server.

How to config the default root index in hosting server

I wanted to put the index.php file of my website in a public file on my server, but consequently, when I tap the url, it is not directing me to the index, but to the folder which contains all of the folder of my website.
I try to put a .htaccess file in the root of the folder containing my website. It worked with my local virtual host wampserver, but not with the real host (where I put files with filezila). I show you the line code I wrote in .htaccess, which worked with wampserver, but not with the actual used server for internet ::
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ public/index.php [QSA,L]
I try also to put the absolute path but same thing. Does someone have an idea about what I have to do to config the index path of my server ? (for a subdomain) ?
I found the solution. I had to remove the first line:
Options +FollowSymlinks
My server already had it activated and didn't support the instruction twice.

How to change the first segment of a URL and maintain the rest of the segments in htaccess

I have pages in my site where the url segment are automatically filled depending on the values of the fields in the page.
And example url is:
http://example.com/students/uk/nikki/86/18-25
where nikki/86/18-25 are dynamic segments.
What I was to do is to simple make an htaccess redirect rule where if a user will go to:
http://example.com/students/uk/nikki/86/18-25
they will automatically be redirected to:
http://example.com/student/uk/nikki/86/18-25
So it's just to make the students segment to student. And since the nikki/86/18-25 segments changes per page, they would be retained during the redirect.
Is this possible in htaccess?
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^students/uk/([^/]+) student/uk/$1 [L,R,DPI]
Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Yii framework: Using htaccess for site in sub directory not working on Hostgator

I'm trying to set up Yii site to be loaded from sub directory to root domain. In my root site folder I have only root .htaccess file and sub directory "subdir" which contains Yii site. I found a solution that works on my local environment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
But when I upload the site to HostGator it just does not work correctly
For example
if I use http://localhost/contact on my local environment, correct page is opened (called site/contact - site controller and contact action. I've added Yii 'contact'=>'site/contact' rule to the urlManager, so both http://localhost/contact and http://localhost/site/contact can work)
If I use http://mydomain.com/contact or (http://mydomain.com/site/contact) on HostGator, I get default index page (called site/index - site controller and default index action instead)
When I choose to access subsites directly like http://mydomain.com/subdir/contact it works fine, but it does not work if I use http://mydomain.com/contact
I guess that I need somehow to change this last rule in htaccess, but not sure how. Thanks!
I've found alternative solution that works. I was forced to use old fashioned URLs instead of paths, so I've changed my urlManager to:
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=>true,
'caseSensitive'=>false,
'rules'=>array(
...
)
)
And my root htaccess looks now like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/index.php?r=$1 [QSA,L]
So, when accessing
http://mydomain.com/site/contact
Actual mapped URL is
http://mydomain.com/subdir/index.php?r=site/contact
which as result returns correct contact page
Since, you stated in your previous question that www.mysite.com/mysite/frontend/www/controller/action works fine; you should be using:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)$ /mysite/frontend/www/$1 [L]
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

How can I get my FuelPHP application working in a subdirectory?

I have a FuelPHP application that I'm trying to setup to extend an existing website (we'll call it example.com). The FuelPHP installation is in a directory called listings within the DocumentRoot. However, the main index.php file for the application is located in a subdirectory of listings called public, so the actual path to the index.php file is <DocumentRoot>/listings/public/index.php. The static assets (JavaScript, CSS and images) for the application are in subdirectories of the public directory as well.
I want people to be able to access the application at /properties. Also, there will be requests made for other pages (such as /properties/admin) as well as requests for static assets (such as /properties/assets/css/style.css).
I've got this thing about 90% working. The .htaccess file in the DocumentRoot looks like this:
RewriteEngine On
RewriteRule ^properties/?(.*)$ /listings/public/index.php/$1 [L]
Inside the public directory, there is another .htaccess file that looks like this:
Options +FollowSymLinks -Indexes
RewriteEngine on
# Send request via index.php (not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I set the $base_url in the FuelPHP config to http://example.com/properties/. All of the URLs for the static assets are appearing the way I would expect them to, for example, the path to my bootstrap.css file appears as http://example.com/properties/assets/css/bootstrap.css. This is what I expect. However, instead of Apache grabbing the static asset, it's running the request though the FuelPHP index.php file, resulting in a 404.
I think I need to add a RewriteCond to the .htaccess file that is in the DocumentRoot, but I'm not exactly sure if that's correct, or what that RewriteCond would look like.
How do I adjust my .htaccess file(s) so that I'm able to access static assets such as http://indyapm.com/properties/assets/css/bootstrap.css which is actually located at <DocumentRoot>/listings/public/assets/css/bootstrap.css?
I believe what you want is a RewriteBase
RewriteEngine On # after this
RewriteBase /properties/ # insert this
That, having your main config set up with the correct base url should do the trick.