how to redirect from a directory to a file that has the same name as the directory (if it exists)? - .htaccess

Imagine: there is example.com/test (directory), and there is example.com/test.html (file)
How to make it so that the link example.com/test opens exactly the file, and not the directory (it is desirable that there is no .html in the address bar)?
I try this:
DirectorySlash Off
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$0.html -f
RewriteRule .+ /$0.html [L]
but it redirects me to example.com/home/a0987901/domains/example.com/public_html/test/, but not example.com/test.

Related

Forcing to append text to the end of a file name with .htaccess

Currently i have this directory:
example.com/curriculums/34-william.pdf
example.com/curriculums/168-romina.pdf
34-william.pdf and 168.romina.pdf are actual files, and they do exist, but they should only be accesible if accessed using these urls:
example.com/curriculums/34-william-cv.pdf
example.com/curriculums/168-romina-cv.pdf
So that:
If a visitor opens example.com/curriculums/34-william.pdf it should return a 404 response code
If a visitor opens example.com/curriculums/34-william-cv.pdf the server should return the contents of example.com/curriculums/34-william.pdf
Any ideas? Thanks!
Assuming you don't have any .htacecss inside curriculums directory, you may use these rules inside your site root .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^curriculums/[\w-]+(?<!-cv)\.\w+$ $1$2 [L,NC,R=404]
RewriteRule ^(curriculums/[\w-]+)-cv(\.\w+)$ $1$2 [L,NC]

How to always redirect to subfolder using htaccess

I want to redirect visitors who are viewing index (not other files) to a sub folder in every conidition. At the moment I'm using the code below in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteRule ^$ /dir [L,R=301]
</IfModule>
But it only works on the main domain but doesn't work when we have the script itself in a subfolder. How should I modify this code to works everywhere?
The previous code works like below when installed on the root folder:
http://www.domain.com => http://www.domain.com/dir
Which is fine, but when we have it on sub, it works like below:
http://www.domain.com/sub => http://www.domain.com/dir
That means it move one level up and then locate for dir and that's the failure.
Use that:
# Change in subfolder:
RewriteBase /sub/
RewriteRule ^$ dir [L,R=301]
In root .htaccess for each subfolder:
RewriteRule ^([^/]+)/$ $1/dir/ [L,R=301]

Trail slash with multiple htaccess

I have directory structure like this:
public_html
.htaccess[1]
-apps
-.htaccess[2]
-admin
-myviwo
when I request http://localhost/mysite/admin it redirect me to http://localhost/mysite/apps/admin/ and shows me the content of the admin directory, if I request http://localhost/mysite/admin/ it doesn't redirect me but it shows me the content of admin directory again which is correct. But I want:
http://localhost/mysite/admin
http://localhost/mysite/admin/
Both of the above URLs shows me the content of admin directory without redirecting me.
.htaccess [1]
RewriteEngine On
RewriteBase /
RewriteRule (.*) apps/$1 [L]
.htaccess [2]
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?(.*)$ admin/$1 [L]
RewriteRule ^(.*)$ myviwo/$1 [L]
How can I achieve this?
In the admin directory, add an htaccess file with the following:
DirectorySlash Off
This makes it so mod_dir won't redirect requests for the directory admin. However, note that there's an important reason why directory slash is "on" by default:
Security Warning
Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.
If that's ok with you, then that's all that you need.
Otherwise, you may need to add a special rule specifically for admin. In the .htaccess[1] file, add right below the rewrite base:
RewriteRule ^admin$ apps/admin/ [L]
EDIT: to make the above rule dynamic, you need to first check if it's a directory:
RewriteCond %{DOCUMENT_ROOT}/apps%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ apps/$1/ [L]

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

How to modify .htaccess so that it points to a sub-folder in codeigniter

Codeinginter provide .htaccess file as explained below. It is from CI User Guide.
However when I add this file in xampp/htdocs/mycodeigniter, and point to http://127.0.0.1/mycodeigniter/somepage, it goes to http://127.0.0.1/xampp which is the root file in htdocs.
How can I fix the file so that it points to http://127.0.0.1/mycodeigniter/somepage?
++++++++++++
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.
Try a relative substitution path:
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
You may as well want to exclude the css and javascripts files from the redirection
Replace this : RewriteCond $1 !^(index.php|images|robots.txt)
With this : RewriteCond $1 !^(index.php|images|robots.txt|stylesheets|javascript)

Resources