Create URLs without .php at the end in Apache - apache

I have pages named help.php, about.php and the address will look like:
www.example.com/help.php
I would like to convert it to:
www.example.com/help/
Is there a way to do this without using .htaccess?

You could create a folder called help and move help.php into it as index.php. Do the same thing for about. So you end up with:
help/index.php
about/index.php
Which would both be reachable at just
www.example.com/help/
www.example.com/about/

No there isn't a way to do this without .htaccess.
You could change the 'associated extenstion' for php from .php to something like .p but you would have to update your apache conf file to reflect that (it would just tell the system that .p files should be treated as .php files).
Another alternative would be to stick your help.php file into /help/index.php, which would let you call it as http://domain.com/help/ as the index.php file would be default loaded.
But that isn't what you were asking for...
So short answer is no, cannot be done without .htaccess.

Related

htaccess redirection implied html extension not working

I've tried many things before coming here, it should be a simple problem, but there is something I miss for sure.
I want to redirect a bunch of URLs to another ones, one by one, and here is an example in my .htaccess file :
RewriteEngine On
Redirect 301 /index.php/Microcontrôleurs_Généralités https://newdomain.org/Microcontrôleurs_Généralités
The thing is that the old URLs are files in a real folder "index.php" but with ".html" extension.
When I go to https://olddomain.org/Microcontrôleurs_Généralités, apache serves me the implied .html file. I can go to https://olddomain.org/Microcontrôleurs_Généralités.html too, it's the same file on disk.
But my redirection as above does not redirect anything.
If I add the .html extension to the file like this :
RewriteEngine On
Redirect 301 /index.php/Microcontrôleurs_Généralités.html https://newdomain.org/Microcontrôleurs_Généralités
Then, if I go to the URL with explicit ".html" at end, it is redirected correctly, but if I miss the .html, apache says the URL was not found.
I've turned this in my head numerous times, I can't figure out the real problem.
Help would be much apreciated, thx.

Shutting down a website using HTaccess does not work

OK, it's very simple but it does not work. I have a wiki site where the root contains an index.php file and the subdirectories contains the content of the wiki (I use PMwiki, so no database is required)
I want to temporarity shutdown the website and make it unaccessible by using an nice HTML page to display the shutdown message. I could rename the index.php file, but the rest of the files in the subfolder will remain accessible.
The first thing that worked but which is not elegant is restricting the whole site with a password in the htaccess using "Require valid-user" and all it's other command. The problem is that I cannot display a nice shutdown message as an HTML file.
Else I tried renaming the index.php file to something else like site.php. Creating a index.html file as a message and using a script like this:
Order Deny, allow
Deny from all
<File "index.html">
Allow from all
</File>
In that case, the index.html file is accessible, but it must be manually typed in the URL, it will not use this file by default. I tried adding DirectoryIndex directive like this
DirectoryIndex index.html
But it still does not work.
So first is there a way to make the user only see 1 page in particular and block everything else.
Second, doing so makes the site unaccessible to me. So is there a way to passords restrict the whole directory structure except for a specific index.html file. So that I could type url/site.php and be able to enter my website using an htaccess password.
Thanks for any help
Just this rule in root .htaccess should be able to handle this:
RewriteEngine On
RewriteBase /
RewriteRule !^shutdown\.html$ shutdown.html [L,NC]
Now you can keep custom HTML content in /shutdown.html. Keep in mind you need to use inline css/js since it will also rewrite css/js requests to /shutdown.html file.

Getting Apache HTTP Server to fill in the file extension

I'd like to let people access files on my root domain directory without having to specify the file extension.
So, for example, there is currently a z9.html that a browser can access with www.mysite.com/z9.html. I would like to let people put in www.mysite.com/z9 to get the file.
The pecking order would be to look for a file of the name submitted with a .php extension, and then, if none found, look for a file of that name with a .html extension.
I don't know why my question was downvoted. It seems like a perfectly reasonable question for the Apache group. The answer is to use Apache's mod_rewrite:
To map any filename without an extension to that filename + .html place the following in .htaccess:
RewriteRule ^/([^.]+)$ /$1.html [L]

How to make a static site look dynamic - .htaccess in apache

I have a site that I am running in Apache that is static and I want the server to treat each file as a directory. I have this thing set up in .htaccess that will get rid of all .html extensions. So this is what I want:
Ex.
http://example.com/about (Currently)
to
http://example.com/about/
without having to change it into a directory or make it dynamic. Is there a .htaccess hack? Is it possible?
You should check out mod_rewrite which lets you rewrite any url using regular expressions.

Changing a file's URL without physically moving it

I have a site, running Linux + Apache.
I have a file in my root directory, let's say file.php.
I want the URL to the file to be "domain.com/newdir/file.php", but I don't want to actually create the newdir and move the file there because it would be a huge hassle to update many many links all over my site.
Is there a way to accomplish this, meaning making the file accessible by the new URL without moving it?
Thank you.
On this site: workwith.me, you can find information about .htaccess and mod_rewrite. For your example you have to make a file called .htaccess and put it in the root directory. The file should contain these directives:
RewriteEngine on
RewriteRule ^newdir/file.php$ /file.php [L]
You can do this for every file you want to rename.
Four possible solutions I can think of:
If your OS supports it, create a symlink:
mkdir /home/foo/htdocs/newdir
ln -s /home/foo/htdocs/file.php home/foo/htdocs/newdir/file.php
... and make sure Apache is configured to follow them:
Options FollowSymLinks
Create an Alias or AliasMatch (probably overkill)
Good old mod_rewrite:
RewriteEngine One
RewriteRule ^newdir/file\.php$ file.php [L]
Ugly: use a custom 404 error page with a PHP script that checks $_SERVER['REQUEST_URI'].
I guess the standard solutions are #1 and #3.