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.
Related
I have to create a page handler, which should read the URL, and do specific operations based on querystring.
I'd need to use .htaccess to do some URL rewriting thing to point everything at a certain file which does the processing, in such a fashion:
https://example.com/folder/page1/
https://example.com/folder/page2/
And the processing file is https://example.com/folder/index.php
Is there any way to do that (possibly by removing the index.php part)?
try this out and see if this helps you achieve what you are looking for.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
The above code is taken for reference from WordPress and it should work when the "RewriteBase /folder/" folder name is updated with your folder and .htaccess file has to be placed in the root/folder directory where your index.php file is located.
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.
I want to point all subfolders and non-existent files to root folder.
www.domain.com/folder/david to www.domain.com/folder/
or
www.domain.com/folder/david/ to www.domain.com/folder/
You can try this in your /.htaccess file:
RewriteEngine on
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, redirect it to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
The NC flag makes the rule case-insensitive. If you do not need it, you can remove it.
If you would like to make the redirect permanent, exchange the R flag for R=301.
If it doesn't matter whether or not it exists, use the following instead:
RewriteEngine on
# Redirect everything to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
Update
Based on your comments, it appears you are running an application in /folder/. As such, it would be better to place your .htaccess file in /folder/, and fill it with the following:
RewriteEngine on
RewriteBase /folder/
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, rewrite it to index.php
RewriteRule ^ index.php [NC,L]
You then do not need an .htaccess file inside the root - only the one in /folder/ is needed.
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.
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.