Remove file extensions using htaccess in subdirectories - linux

I'm trying to remove file extensions with htaccess, so www.mysite.com/file.php becomes www.mysite.com/file.
I'm using the following code in the .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So far, so good. Where it falls down is in subfolders: www.mysite.com/subfolder/file.php
becomes www.mysite.com/file.
I've tried uploading another .htaccess file in the subfolder, but it still does the same. It feels like it should be really simple, but I'm struggling...can anyone help me out? Thanks!
Edit Sorry folks, should have said - the file is in a subfolder like so:
www.mysite.com/folder/subfolder/file.php
The .htaccess file is in /folder, the URL changes to this format:
www.mysite.com/subfolder/file
Apologies for misleading.

This is the rule you'll need to hide .php extension. This goes into your .htaccess in the DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

You shouldn't be using rewrite rules for this. Apache has built-in option explicitly for doing what you're trying to do, called MultiViews, and that's what you should be using:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
Just add Options MultiViews to your .htaccess, and make sure that AllowOverride is properly configured.

A guess in the wild
Try
RewriteRule ^(.+)$ $1.php [NC,L]

Related

htacces rewrite single file

I want to rewrite a specific file on my website to another one by using htaccess:
# General
RewriteEngine On
RewriteBase /
Options All -Indexes
Options +FollowSymLinks
# Rewrite file
RewriteRule ^/file.html$ /dir/file.html [L]
This is the .htaccess code i'm using based on snippets i found on the internet.
Somehow this is not working, the server is returning a 404-Not-found error.
I can't see any difference with example's that are said to work, like in
Rewriting path for a specific file using htaccess
Edit:
If I place file.html in the root-folder, I can view it. So the rewrite definitely is not happening.
RewriteRule does not receive leading slash. Write so:
RewriteRule ^file.html$ /dir/file.html [L]

Is this .htaccess rewrite even possible?

My co-worker is proposing a .htaccess rewrite rule to implement on our system. I'm not even sure if it's possible, and I want someone more educated than me on the subject to verify.
Here's the URL currently:
http://cms.phasesolutions.ca/themes/Default/page.php?slug=home
Here's how he wants it to show up:
http://cms.phasesolutions.ca/pages/home/
Is that possible? (where the variable for "?slug=", and the "Default" folder, are subject to change, depending which theme they're accessing and what slug they're specifying)
Let me know,
thanks
Posting a solution in case you need starting point.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# handles /Default/home/ to /themes/Default/page.php?slug=home
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /themes/$1/$2.php?slug=home [L,QSA]

Rewrite 1 level of url directory

I'm looking to rewrite the first directory of a url string and have the rest of the request still work.
Eg: I want it so when a user clicks the link for : /products/category/item.php
it actually grabs the file of : /shop/category/item.php But still shows as /products/category/item.php as the URL
This will be dynamic so it should be something like /products/$ /shop/$1 I'm guessing.
You do not need mod_rewrite. when to avoid mod_rewrite.
Mapping url directories to file directories is a basic functionnality of Apache handled by the mode mod_alias (which is quite certainly already present for you).
So basically you have the Alias and AliasMatch directives. In your case the first one is enough:
Alias /products/ /path/to/web/document/root/shop/
The mapping is done only server-side so the url seen by the end user is never modified.
Try this:
RewriteEngine On
RewriteRule ^products/(.*) shop/$1 [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/(.+)$ /shop/$1 [L,NC]

URL-rewriting with index in a "public" folder

I’m a newcomer in the development world. I desperately try to get the good URL. I checked the site for similar problems but I can’t find exactly what I need. Or I do it badly.
Here is the situation: I set up a project for a site whose the index.php file is in a folder named Public.
To be clearer, here is the URL I have now to reach the homepage of the built site:
http:// Domain Name.com/ Folder / Name of the site/public
My concern is about the folder Public: I don’t want it appears in the URL.
Here is the URL I’d like to get:
http:// Domain Name.com/ Folder / Name of the site
In fact, I’d like this URL permits to get the index file placed in the folder "Public".
I can’t access the Apache configurations (shared host) so I have a .htaccess I placed in the project (i.e: www/ Folder /Name of the site /.htaccess). Here is its content:
Options -Indexes +FollowSymLinks +Multiviews
RewriteEngine On
RewriteBase /public/
RewriteRule ^(.*)$ index.html [NC,L]
I made something very simple for now because I tried lots of things without efficient result.
Not really sure what you are trying to do, but if you want to remove the /public/ path that appears in the URL, you need to remove it from all your links, second, turn off multiviews, it's not what you want, third, you need a rule to externally redirect the browser when a request is made for /public/, then you need to internally rewrite requests to point to public.
Options -Indexes +FollowSymLinks -Multiviews
RewriteEngine On
# externally redirect, must match against %{THE_REQUEST}
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /public/
RewriteRule ^/?public/(.*)$ /$1 [L,R=301]
# internally rewrite it back, but we must first check that it's pointing to a valid resource:
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -s
RewriteRule ^(.*)$ /public/$1 [L]

redirect request uri to mirror site using htaccess

I have a website. I created a mirror of it and uploaded it in a directory called 'mirror'. What I wanted to do is whenever a viewer access for example..
http://www.example.com/this-page/another-segment/?id=1
I want him to be redirected to..
http://www.example.com/mirror/this-page/another-segment/?id=1
^^^^^^
(I am doing this because I want to want to edit my site's design but I don't want viewers to see the changes in progress until they are complete. Thus I want to redirect them to the mirrored snapshot, at least temporarily.)
Please suggest how this can be done using .htaccess or not.
after browsing around the internet. i came up with the idea of putting a /$1, a rewritebase and followsymlinks on the answer givien by appclay
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule (.*) /mirror/$1 [R=301,L]
You'll want to do something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule ^/(.*) /mirror/$1 [R]
In your .htaccess file.

Resources