remove file names completely from url with .htaccess - .htaccess

With some modifications is possible to hide extension of the web files like http://www.abc.com/asd/zxc/ against to zxc.php but the thing I wanna do is remove file names completely from the url like http://www.abc.com/asd/ it doesn't matter user where to go in web site but the url should be stay static all the time.
Is it possible to do that with .htaccess?
I already tried this but it didn't work:
RewriteOptions inherit
RewriteEngine On # enables url rewriting
RewriteCond %{REQUEST_FILENAME} !-d # if requested uri is not directory (!-d)
RewriteCond %{REQUEST_FILENAME}\.php -f # and if there is a file named URI+'.php' (-f)
RewriteRule ^(.*)$ $1.php # then if there is any thing in uri then rewrite it as uri+'.php'

I'm not sure, but isn't the problem that you would like to check so the supplied url fragment is not a directory and not a file, and if that's the case, append .php to the fragment?
Something like this might work:
RewriteEngine On # enable mod_rewrite
RewriteBase / # set the 'base' for the rewrite
# you might need to modify this one.
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteCond %{REQUEST_FILENAME} !-d # not a directory
RewriteRule ^(.*)$ /$1\.php [L] # append '.php' to the path
#
If you have the following directory structure, and of course the .htaccess file in the root of the structure:
/code/test.php
/index.php
You should be able to access the test.php and index.php files by using the following urls:
http://example.com/code/test/
and
http://example.com/index/
The example.com needs to be change to a valid domain or ip address.

Related

Remove public folder from URL with htaccess

Is it possible to 'remove' a folder from the URL so if somebody types http://www.example.com/dummy/public/index into their browser address bar, I can strip the 'public' folder, so the URL reads http://www.example.com/dummy/index
I essentially want to hide the 'public' folder so it never appears in the URL.
I have this htaccess at the root of my site which is www.example.com/dummy/
RewriteEngine on
RewriteRule ^(.*) public/$1 [L]
This is the htaccess inside my public folder which is located at www.example.com/dummy/public/
Options -MultiViews
# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On
# Prevent people from looking directly into folders
Options -Indexes
# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I tried changing the last RewriteRule to RewriteRule ^(.+)public/(.*)$ index.php?url=$1$2 [QSA,L] but when I type www.example.com/dummy/public/index the page loads but the browser address bar still shows 'public' in the path.
Is it possible to do what I'm attempting?
I've seen a few SO answers that claim to accomplish this such as .htaccess: remove public from URL and URL-rewriting with index in a "public" folder, but none of them work for me.
If you want to remove public/ from your URLs then rule should be placed inside /public/.htaccess since all the requests with URI starting with /dummy/public/ will be managed by rules inside /public/.htaccess.
Change your /public/.htaccess with this:
# Prevent people from looking directly into folders
Options -Indexes -MultiViews
# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On
# remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
RewriteRule ^ /%1%2? [R=301,L,NE]
# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
# then rewrite the URL in the following way:
# Take the whole request filename and provide it as the value of a
# "url" query parameter to index.php. Append any query string from
# the original URL as further query parameters (QSA), and stop
# processing this .htaccess file (L).
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Fear not! For the answer is yes. Use the following rule in your .htaccess to remove the /public/ folder from your URLs:
RewriteEngine On
RewriteRule ^(.*)/public /$1 [R=301,L]
This will leave you with the desired URL of http://www.example.com/dummy/index and it achieves this using a 301 permanent redirection. For testing purposes I suggest you change this to 302 as this will make it temporary, once happy, change it back.
Make sure you clear your cache before testing this.

skip directory in URl by using Rewriting Rules

I have a web-page with is located in example/files/page.php in my server. I would like to use Rewrite Rules in order to skip the /files directory in the URl so when the user types example/page.php to be able to view the content of the page.php. How is it possible to do so with Rewrite Rules?
Add this to the .htaccess in the root directory of your website.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(example)/(.*)$ /$1/files/$2 [NC,L]

Execute index of a folder if exists, if not execute php file

I want this example URL:
example.com/folder/phpfile
To redirect to:
example.com/folder/phpfile.php
It works perfectly, but I also want to be able to execute the index.php file of any folder just by this URL
http://example.com/folder
or
http://example.com/folder/
or maybe
example.com/folder/subfolder
But I don't know how, I'm using RewriteCond %{REQUEST_FILENAME}, as I understand this is a condition to know if the requested file name is either a file or a folder. But I can't make it work.
This is my current .htaccess:
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-_])$ $1.php [NC,L]
You can meet your requirement using this code in root .htaccess:
# by default load index.php from a path
DirectoryIndex index.php
# make sure trailing slash is present for directories
DirectorySlash On
RewriteEngine On
# load /dir/file.php if request is for /dir/file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
here is one more thing that we can do to further clean our URLs, i.e., hiding the entry script index.php in the URL. This requires us to configure the Web server as well as the urlManager application component.
We first need to configure the Web server so that a URL without the entry script can still be handled by the entry script. For Apache HTTP server, this can be done by turning on the URL rewriting engine and specifying some rewriting rules. We can create the file /wwwroot/blog/.htaccess with the following content. Note that the same content can also be put in the Apache configuration file within the Directory element for /wwwroot/blog.
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
We then configure the showScriptName property of the urlManager component to be false.
Now if we call $this->createUrl('post/read',array('id'=>100)), we would obtain the URL /post/100. More importantly, this URL can be properly recognized by our Web application.

Root folder change (.htaccess)

In my "public_html" directory I have the following structure:
- root
- index.html
- blog
- index.html
- lab
- index.html
- wp
- (WORDPRESS FILES)
The "lab" and "wp" directories are just subdomain directories ("http://lab.tomblanchard.co.uk" and "http://wp.tomblanchard.co.uk") which work fine.
Basically I want the main domain ("http://tomblanchard.co.uk") to point to the "root" directory without any actual redirecting, for example, I want "http://tomblanchard.co.uk" to point to the "index.html" file within the "root" directory, I want "http://tomblanchard.co.uk/blog" to point to the "index.html" file within the "root/blog" directory and so on.
I have kind of achieved this with the following code in my ".htaccess" file:
# Add directives
RewriteEngine on
# Remove ".html" extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Change root directory to "root" folder
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} !(.*)root
RewriteRule ^(.*)$ root/$1 [L]
The only problem is that things like "http://tomblanchard.co.uk/root/" and "http://tomblanchard.co.uk/root/blog/" still work when really they shouldn't even be able to be accessed (404).
If anyone has any idea on how to sort this or has a stronger method of doing this it would be greatly appreciated.
Update
Finally got it working how I wanted it after hours of researching, I used the following:
# Add directives
RewriteEngine on
# Change root directory to "root" folder
RewriteCond %{THE_REQUEST} ^GET\ /root/
RewriteRule ^root/(.*) /$1 [L,R=301]
RewriteRule !^root/ root%{REQUEST_URI} [L]
The order of directives in mod_rewrite is important, as each rule sees the output of the previous rule as its input to test. You need to do 3 (or possibly 4) things, in order:
Deny access to any URL beginning /root/ (we have to do this first, else everything will be denied!)
It's generally good practice to ensure each URL has only one valid form, so URLs which do specify .html should cause a browser redirect to the non-.html form. This needs to happen before other rewrites, otherwise you can't tell the difference between a .html from the browser and one you've added virtually.
Look up any URL not denied above in the /root/ directory, rather than the configured DocumentRoot
Look up any URL not pointing at a directory under the URL + .html, if that file exists. This has to come after other rewrites, or the "file exists" check will always fail.
# General directives
Options +FollowSymLinks
RewriteEngine on
# Deny URLs beginning /root/, faking them as a 404 Not Found
RewriteRule ^root/ [R=404]
# Additional rule to strip .html off URLs in the browser
RewriteRule ^(.*)\.html$ $1 [R=permanent,L]
# Rewrite everything remaining to the /root sub-directory
# (Host condition was in your post originally, then edited out; this is where it would go)
RewriteCond %{HTTP_HOST} ^(www\.)?tomblanchard\.co\.uk$
RewriteRule ^(.*)$ root/$1
# Handle "missing" ".html" extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
PS: Note my careful language to describe (internal) rewrites, as opposed to (browser) redirects: the rule you have is not removing .html from anything, it is adding it, thus allowing the page to be accessed if someone else removes it. Since you are often modifying both within a set of rules, it's important to keep clear in your head the distinction between the URL the browser has requested, and the virtual URL Apache will ultimately serve.
You are not defining any rule to block /root address so how do you want to block it when there is nothing to do that?
Try this:
# Add directives
RewriteEngine on
RewriteCond %{REQUEST_URI} .root [NC]
RewriteRule (.*) / [L,R=404]
# Remove ".html" extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Change root directory to "root" folder
RewriteCond %{HTTP_HOST} ^tomblanchard.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.tomblanchard.co.uk$
RewriteCond %{REQUEST_URI} !.root
RewriteRule (.*) /root/$1 [L,R=301,QSA]
This is not tested so if it wouldn't work, play around with it to get your need.

htaccess redirecting issue

My current .htaccess:
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/kohana/, use /kohana/
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
When someone navigates to:
http://mysite.com/demo/test
I want them to go to the actual folder. When someone navigates to:
http://mysite.com/demo
I want them to go to index.php/$1.
How can I achieve this?
Your rule should work as it is only applied if the requested URL can not be mapped to an existing file (!-f) and not to an existing folder (!-d). So if /demo/test is an actual folder, the second condition (RewriteCond %{REQUEST_FILENAME} !-d) should fail. And if /demo is requested and is neither an existing regular file nor an existing folder it should be redirected to /index.php/demo.

Resources