Rewrite rule to redirect to file and not folder with same name - .htaccess

I am working on a new website and I want the following url /newsite/products to redirect to /newsite/products/product.php.
My .htaccess file is as follows:
RewriteEngine On
RewriteRule ^newsite/products$ /newsite/products/product.php
My problem is that there is already a folder called products within newsite and when requesting newsite/products it tries to open that folder rather than go to the php file.
I get:
Forbidden
You don't have permission to access /newsite/products/ on this server.
Is there any way to make this work?
EDIT: Requested url is: http://example.com/newsite/products and location of .htaccess is in root.

You need to turn off the directory slash to rewrite your file :
Try :
DirectorySlash off
RewriteEngine On
RewriteRule ^newsite/products$ /newsite/products/product.php [NC,L]
Don't add a trailing slash in rule pattern
/newsite/products
will rewrite to
/newsite/products/product.php
and
/newsite/products/
will open your "products" directory
Note- this is not a safe solution, if you remove the rule and DirectorySlash is off, Your index file will be ignored ,all directories will start listing files and folders.

Related

Rewrite engine not working using htaccess

I am trying to rewrite url using htaccess but it's saying 404.
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ product1.php?pid=$1
It's not opening product1.php file.
You need to make sure your htaccess file and your product1.php file are in same folder. If not that it will give 404 errors.
If they are not present in same folder then you need to complete relative path from the path where your htaccess file is present like: eg: Let's say we have folder structure like:
/root/.htaccess
/root/singh/test/product1.php
So have your rules in following manner then:
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ singh/test/product1.php?pid=$1 [NC,L]
Also apart from these put NC and L flags too in your rules to do case sensitive matching and making this condition's last rule here.

Redirect existing second page to non-existing url

I have a site https://sitename.com/web (example). So, full path to page is sitename.com/web/index.php. On ftp server there is folder /web which also contain second page file - index-2.php. How to avoid creating /web-2/ folder and redirect index-2.php to sitename.com/web-2 ?
Easy, create an .htaccess file inside the root folder of your site, then add the following lines to it:
RewriteEngine On
RewriteRule ^web-2$ index-2.php [QSA,L,NC]
This site might help you with htaccess:
https://www.htaccessredirect.net/
A nice tutorial on .htaccess files also below:
https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file

Redirect everything to 404 except files in public folder

How redirect everything inside rootfolder to 404, except files in root/public folder? Directory listing must be disabled everywhere and .htaccessredirected to 404 as well. It's possible to make that with one .htaccess file?
Currently i have Redirect 404 / in root, but when trying to access .htaccess, it's forbidden. Thanks
To redirect everything inside rootfolder to 404, except files in root/public folder ,you can use the following Rule in your /root/.htaccess .
RewriteEngine on
RewriteRule !public - [R=404,L]
and to disable directory listing for all folder/subfolders, add this line to your /root/.htaccess
Options -Indexes

When trying to redirect using HTACESS, a string "/redirect:/" gets embedded with the url

I have setup two domains to point to the same hosting server. For eg: the domains be :
example1.com
example2.com
I have created a directory called 'home' in my root, and whenever the 2nd domain is accessed, it should get redirected to the home directory.
For eg: example2.com/post.php should get redirected to example2.com/home/post.php
I have done this by creating an htaccess file in my root directory with the following contents:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteCond %{REQUEST_URI} !home
RewriteRule ^(.*)$ ./home/$1
This is working perfectly. Now, i have a second requirement where i need to redirect users to a php file (post.php) inside home directory whenever the user accesses the url example2.com/gallery/somestring.
So, i created a second htaccess file in my home directory and entered the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^gallery/([^/]+)$ post.php?post=$1
But, whenever i try to access the url example2.com/gallery/somestring, it shows a strange 404 Not Found error :
The requested URL /home/redirect:/home/gallery.html/somestring/somestring was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Here. "redirect:" string is getting concatenated to the redirected URL somehow.
Can any one please give me some idea as to why its happening ??
I solved this problem. I am sharing this if any one gets into this same problem and finds this post. The problem was that there was a file gallery.html in my root folder. This was causing this error. Just delete this file or rename the file to some thing else. Thats it :)

simple .htaccess rewrite URL to different directory on same server

Ok, I'm clueless here...
I need to rewrite a directory structure and all sub-directories within it to a directory within the same server, but a root that is before the directory.
For example:
http://www.mydomain.com/Themes/default/css/folder
and all directories called upon after folder. Such as folder/sub_folder or folder/afolder/anotherfolder, it needs to include ALL sub-directories within the folder directory.
should be redirected to this:
http://www.mydomain.com
How do I do this via a .htaccess file within the folder path http://www.mydomain.com/Themes/default/css/folder?
Please someone help.
Thanks guys :)
The files within the directory structure still need to be accessible for that structure when called via PHP, but I don't want people being able to browse to http://www.mydomain.com/Themes/default/css/folder and be shown all subdirectories within that folderpath and/or all files. Same thing for all sub-directories that follow that folder path.
I'd like to be able to place the .htaccess file within the http://www.mydomain.com/Themes/default/css/folder directory on the server, but don't know exactly what code to use for this.
ALSO, even more challenging... The domain name can change, so I'd rather not use the domain name within the .htaccess file, instead perhaps use .. or . to go up a directory or a different method of grabbing the domain name within the .htaccess file.
Create a .htaccess file in /Themes/default/css/folder and place these lines there (it requires mod_rewrite):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ http://%{HTTP_HOST}/ [R=301,L]
It will redirect (301 Permanent Redirect) all requests to a folder to a homepage. If file is requested, it will allow it.
If you want to have it working for folders as well as files then remove the RewriteCond line -- it will redirect ALL requests (even for non-existing URLs) to a homepage.
If you will see "500 Internal Server Error" after creating such file, then it is your server configuration: mod_rewrite may not be enabled or it's directives (RewriteRule, RewriteCond, RewriteEngine) are not allowed to be placed in .htaccess. In any case -- check Apache's error log for exact error message (it will give you the exact reason).
http://www.besthostratings.com/articles/prevent-directory-listing.html
IndexIgnore *

Resources