.html removal from URL using .htaccess rewrite rules - apache

I have a static website - a bunch of static html pages. I am trying to remove .html part from the URL of my webpages. I have used an .htaccess file with the following code to do that:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
However, I am getting a 404 error. For example:
The requested URL /home/username/public_html/contact.html was not found on this server.
Ideally, it should redirect to /~username/contact.html.
Addition information
When I used "Options -MultiViews" line above the code it is giving the following error:
500 Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (Red Hat) server is used here.
Why I am facing this problem? Is root is automatically getting changed from public_html/ folder?
EDIT:
Directory Structure:
Username
.gnome2 (there are empty folders inside it)
.mozilla (there are empty folders inside it)
public_html (I have put css, fonts, js, etc folders and .htaccess, contact.html, index.html, etc files here.)
.bash_logout
.bash_profile
.bashrc
Username folder is under universitynameuniverse folder (whose other folders I cannot see).

Try this code in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page.

Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [QSA,L]
</IfModule>
## Results
# ~user/contact => ~user/contact.html (only if file exists)
It only works if the ~user/ directory actually exists on the filesystem.
If you need an external redirect instead, append an R flag to the RewriteRule directive.
The problem with your rewrite rule is that it's appending a .html suffix to the whole %{REQUEST_URI} variable which will probably result in a 404.
Update
Re-reading your question I noted that you want to map the ~user part to somewhere out of the apache webroot. In this case, try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/~(.+)/(.+)$
RewriteCond /home/%1/public_html/%2.html -f
RewriteRule . /home/%1/public_html/%2.html [QSA,L]
</IfModule>
## Results
# ~user/contact => /home/user/public_html/contact.html (only if file exists)

Related

.htaccess Remove filename from url

I'm trying to remove: "site.php" from my url:s, which look like
"example.com/custom-foldername/site.php", so the url:s would look like "example.com/custom-foldername"
Currently it's not working at all. Basically my site just doesn't change the url. I know that the .htaccess file is being read, because if i write junk code in it, it gives error 500.
Here is my current .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options -Indexes
RewriteRule ^site\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site.php [L]
</IfModule>
The code you posted is a front-controller pattern - it rewrites every request for non-existent files to /site.php in the document root (not /custom-foldername/site.php).
And nor will a RewriteRule pattern like ^site\.php$ match a request like /custom-foldername if the .htaccess file is in the document root.
I'm trying to remove site.php from my URLs...
From all your URLs? Is site.php present in all your URLs? So, site.php is your front-controller?
If site.php really is your front-controller then try something like the following instead:
Options -Indexes
RewriteEngine On
RewriteBase /custom-foldername
RewriteRule site\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ site.php [L]
This rewrites everything that doesn't map to a physical file or directory to /custom-foldername/site.php.
UPDATE: I solved this my myself while ago, but forgot to update
So, i found the solution and here's my current .htaccess file, if someone is ever having the same problem
RewriteEngine On
RewriteBase /
Options -Indexes
DirectoryIndex site.php site.html
When user "asks" for folder directory (example.com/cats) apache looks for "site.php or site.html" files inside it. (example.com/cats/site.php) If it finds either on of them it displays it to the user as (example.com/cats). If either file isn't found it gives normal "404 not found error"

Apache 2.4 .htaccess - Point All Requests to index.html With One Exception

I currently have the following .htaccess:
<IfModule mod_rewrite.c>
Options Indexes FollowSymLinks
RewriteEngine On
RewriteBase /production/
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
The web server's document root is currently the /production folder referenced in the above (the .htaccess file is in the parent folder since /production is deleted and rebuilt with every code commit). The above will direct all traffic to my site to index.html.
I would like to make an exception to this. If the request is www.mydomain.com/specialrequest, I would like a PHP script called script.php in the parent folder to run.
To review, this is my /var/www/html directory:
-html
-production
-anotherfolder
-.htaccess
-script.php
Apache is pointing to /var/www/html/production and I would like all requests to go to the index.html file in that directory unless the request is /specialrequest - in which case, I would like script.php to run.
Apache is pointing to /var/www/html/production
So, /var/www/html/production really is defined as the DocumentRoot. And so your .htaccess file is located above the document root. And the file you want to rewrite to (upon receiving this special request) is also above the document root.
Unfortunately, you can't rewrite to a file that is above the document root using .htaccess (regardless of where that .htaccess file is located). This is because the RewriteRule substitution in per-directory .htaccess files takes a URL-path, so trying to rewrite to a URL above the document root is simply invalid. If, however, you had access to the server config (or virtual host) then you could rewrite to a filesystem path (including above the document root) - instead of a URL-path - using this method.
So, script.php would need to be within the document root in order to be able to do this using .htaccess. In which case, you could do something like:
RewriteEngine On
# Exception
RewriteRule ^specialrequest$ /script.php [L]
# Front controller
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
But in this case, script.php is would need to be located at /html/production/script.php (in the document root), not at /html/script.php (above the document root) - as in your current filesystem structure.
Incidentally, the RewriteBase /production/ directive in your .htaccess file is entirely superfluous. You're not using a relative path substitution anyway, but since /production is the document root then /index.html (a document root relative URL-path) will rewrite to /production/index.html anyway.
If, however, you could write this directly in your server config (or virtual host), then you could do what you require. For example:
RewriteEngine On
# Rewrite to filesystem path (in the server config)
RewriteRule ^/specialrequest$ /var/www/html/script.php [L]
# Front controller
RewriteRule ^/index\.html$ - [L]
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^/. /index.html [L]

.htaccess RewriteRule adds drive path to URL

I am using Zend Server CE (v.5.1.0) installed on C: on a Win7 machine. I have added one project to httpd.conf by adding:
Alias /project "D:\Homepages\project"
<Directory "D:\Homepages\project">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
My .htaccess file in the project directory contains the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/\w*\.(css|js) [NC]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now to the problem; if I go to
http://localhost/project/index.php
everything seems to be working fine. I reach the index.php file and get my contents.
However, if I go to any other page that would trigger the RewriteRule, it seems to be adding the directory path. FireFox outputs the following Not Found message:
The requested URL /Homepages/project/index.php was not found on this server.
I tried to find a similar question/answer here, but failed. Any idea?
Ps. Me accepting of an answer might be delayed as I will be out for a while on an errand.
You need to set the RewriteBase directive; otherwise, mod_rewrite automatically, and by default, prepends the file path to the resulting rewrite rule.
From: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix.
If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.
Have your last line like this:
RewriteRule ^.*$ /index.php [NC,L]
However I think this is infinite loop so I would suggest this rule instead:
RewriteCond %{THE_REQUEST} !\s/index.php [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule . /index.php [L]
which prevents going to index.php if it is already /index.php.

Apache ignoring file extensions

I use the following .htaccess code to make my URLs cleaner:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
It basically check if the requested URL points to a file or a directory and if it doesn't, it formats it in a particular way.
The problem is that my production server seems to ignore file extensions when it checks if the requested URL points to a file. For example, it would consider the URL /contact pointing to a file named contact.jpg if a file with that name existed on the root of the server.
What causes Apache to behave like that and what can I do to control it - make it strict about file extensions?
I believe it's because of MultiViews option.
Try Options -MultiViews in the .htaccess

How would I go about creating a mod_rewrite that redirects to launch.php?i=/the/url/that/they/want?

So if the user types mydomain.com/dashboard, the document the server actually sends them is /launch.php?i=/dashboard.
The one caveat is that I would like to leave requests for
/flags
/people
/posters
/css
/icons
/images
/libraries
/patterns
alone, and they should request the actual folder.
How would I create such a mod_rewrite?
This is the .htaccess file for the CakePHP Framework.
Please replace the index.php and ?url= to fit your needs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
The "!-d" tells Apache to follow existing folders and "!-f" to follow existing files.
Everything else is channelled through index.php
As suggested in a comment, you have to be aware that if it's not working it could be because mod_rewrite is not enabled and you'll not get an error stating that fact, you'll probably only have a HTTP 404.