.htaccess Rewrite 403 Forbidden - .htaccess

I have a .htaccess rule for get content from the file partnerzy.php when user types:
https://example.com/partnerzy
and
https://example.com/partnerzy/
at address field. My .htaccess contains:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} /partnerzy
RewriteRule partnerzy.php [NC]
RewriteCond %{REQUEST_URI} /partnerzy/
RewriteRule ^(.*) partnerzy.php [NC]
The rule works on different service, but for my site I get
403 Forbidden
You don't have permission to access this document.
Why it doesn't works ?

You have a syntax issue in your first RewriteRule.
Replace all of your code with this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(partnerzy)/?$ $1.php [L,NC]

The server contains the same folder name like rewrite result /partnerzy/.
Apache doesn't know what to load: partnerzy/ content from partnerzy.php (rewrite rule) or partnerzy/index.(*) and says: 403 Forbidden
There are two ways to solve issue:
make partnerzy/.htaccess with Options +DirList rule,
change dir name partnerzy to another
Then there is no colision with 403 Forbidden

Related

Redirect entire subdirectory to a domain

I am trying to redirect an entire subdirectory to our main domain.
Basically links like this:
https://garrysun.com/dev/ayurveda-products/categories/ayurvedic-ghee-clarified-butter?limit=15
https://garrysun.com/dev/
https://garrysun.com/dev/ayurveda-products/
Should all go to
https://garrysun.com/
We are using OpenCart 2.3.0.2
I have tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/dev/(.*)$
RewriteRule ^(.*) https://garrysun.com/ [R=301,NC]
and
RewriteRule ^dev/(.*)$ https://garrysun.com [R=301,NC,L]
But neither seems to work. What is the best .htaccess rule to make this work?
Inside /dev/.htaccess have this single rule:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?garrysun\.com$ [NC]
RewriteRule ^ https://garrysun.com/? [R=301,L]
Your web server appears to be Nginx as per the response headers. For Nginx use this rule:
location ~ ^/dev/ {
return 301 https://garrysun.com/?;
}
I used this redirect:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /$1 [R=301,L]
The problem is that if the page doesn't exist in the main website I get a 404 error.
For example:
This: https://garrysun.com/dev/777-oil-psoriasis
Becomes this: https://garrysun.com/777-oil-psoriasis
I get a 404 error because that page doesn't exist on the main site.
This works:
https://garrysun.com/dev/ayurveda-products/
Becomes:
https://garrysun.com/ayurveda-products/
because the new site has that page.

Redirecting using Rewriterule from .htaccess

I have a domain (let's say www.example.com) and would like this to point to /Example_folder/ of my server (within /var/www/).
So, if I try to goto www.example.com/images/test.html or example.com/images/test.html, it should be actually pointing at /Example_folder/images/test.html.
I tried to get this working using following code, but I can't figure out.
Trial#1:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/ [L]
If I use above code, I get ERR_TOO_MANY_REDIRECTS.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/index.html [L]
If I use above code (where index.html is specified), it would redirect but I can't get my domain to point at its subdirectories. (www.example.com/images/test.html would also point at www.example.com/index.html)
I got it working using the code from link below:
htaccess Silent Redirect to Subdirectory: Subdirectory showing when no trailing '/'
Last thing that remains is that when I point to www.example.com/Example_Folder, I want the address bar to show www.example.com, but I have not figure that out yet.
You can use this negative lookahead based rule to avoid rewrite loop:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
Rewriterule ^((?!Example_Folder/).+)$ /Example_Folder/$1 [L,NC]
Which means rewrite only if URI doesn't already start with Example_Folder/.

Rewrite URL after redirecting 404 error htaccess

So I know this may seem a little strange but I for sake of consistency, I would like all my urls to appear in this form:
http://example.com/page/
So far I have gotten the regular pages working but I cannot seem to get the error pages working properly.
If the user visits a page or directory that does not exist, I would like the browser to hard redirect to:
http://example.com/404/
This directory, however, will not actually exist. The real location of the error page will be under
/pages/errors/404.php
Also, although I do not need an exact answer for all the various errors (400, 401, 403, 404, 500), I will be applying whatever method is given to redirect all of these to their "proper" URL's
eg.
http://example.com/400/
http://example.com/500/
etc.
Any ideas?
Try this in your .htaccess:
.htaccess
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
The ErrorDocument redirects all 404s to a specific URL, all 500s to another url (replace with your domain).
The Rewrite rules map that URL to your actual 404.php script. The RewriteCond regular expressions can be made more generic if you want, but I think you have to explicitly define all ErrorDocument codes you want to override.
Local Redirect:
Change .htaccess ErrorDocument to a file that exists (must exist, or you'll get an error):
ErrorDocument 404 /pages/errors/404_redirect.php
404_redirect.php
<?php
header('Location: /404/');
exit;
?>
Redirect based on error number
Looks like you'll need to specify an ErrorDocument line in .htaccess for every error you want to redirect (see: Apache ErrorDocument and Apache Custom Error). The .htaccess example above has multiple examples in it. You can use the following as the generic redirect script to replace 404_redirect.php above.
error_redirect.php
<?php
$error_url = $_SERVER["REDIRECT_STATUS"] . '/';
$error_path = $error_url . '.php';
if ( ! file_exists($error_path)) {
// this is the default error if a specific error page is not found
$error_url = '404/';
}
header('Location: ' . $error_url);
exit;
?>
Put this code in your .htaccess file
RewriteEngine On
ErrorDocument 404 /404.php
where 404.php is the file name and placed at root. You can put full path over here.
Try adding this rule to the top of your htaccess:
RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]
Then under that (or any other rules that you have):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
In your .htaccess file , if you are using apache you can try with
Rule for Error Page - 404
ErrorDocument 404 http://www.domain.com/notFound.html

htaccess in a shared hosting

I have a account in a shared hosting(www.1and1.com) and i want a .htaccess redirecting all the request to a different folder. (I'm trying to setup a symfony2 app)
for example, on request to:
http://www.mydomain.com/
internally respond:
http://www.mydomain.com/folder_x/folder_y
i already try this(do not work):
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/(.*)$ /folder_x/folder_y/$1 [R,L]
Thanks
Leading slash isn't matched in RewriteRule in .htaccess. Change your rule to:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder_x/folder_y/ [NC]
RewriteRule ^ /folder_x/folder_y%{REQUEST_URI} [L]
Some other improvements I have made:
Removed R flag since you wanted internal rewrite only
Added RewriteCond before rule to avoid rewriting URIs that already start with /folder_x/folder_y/

Special rewrite rule for .htaccess

Having issues trying to do a proper .htaccess rewrite with the following conditions
original URL:
http://example.com/foo/bar/ANYTHING (either http or https, ANYTHING could be anything the user enters)
rewritten URL:
https://example.com/foo/bar/index.php/this/that/ANYTHING
but don't rewrite anything with index.php already in it.
I've tried several options, but I keep getting the filesystem path included in the new URL.
My attempt:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /foo/bar/
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteCond %{HTTP_HOST} !index.php
RewriteRule ^(.*)$ https://example.com/foo/bar/index.php/this/that/$1 [R=301,L]
First off, test your rules without 301, because the browser caches 301 results and makes testing much harder. Add R=301 not until you're satisfied with the rules.
See also Tips for debugging .htaccess rewrite rules.
You don't need to test for example.com, unless multiple domains are hosted in the same directory.
Testing for index.php is against REQUEST_URI, not HTTP_HOST
RewriteEngine On
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^/?foo/bar/(.*)$ https://example.com/foo/bar/index.php/this/that/$1 [R,L]

Resources