mod rewrite hide directory structure - regex

I moved all the files/folders in the root to a folder v1.0 and used the following:
RewriteCond $1 !^v1.0/
RewriteRule ^/?(.*) v1.0/$1 [L]
This works and the site now accesses files inside v1.0. The problem is when directly accessing the folders.
This: http://example.com/includes (includes exists)
forwards the url to
http://example.com/v1.0/includes and shows this error:
Forbidden
You don't have permission to access /v1.0/includes/ on this server.
How can the folder structure not be exposed, both in the URL and in the error message?

Since you tagged .htaccess, I assume that you have your rule in your .htaccess. In this context, the first argument of RewriteRule will match against an url that will guaranteed NOT begin with a slash, as that is part of the common prefix.
I think the easiest solution is to add custom error pages for forbidden and not found errors. See the ErrorDocument directive for more information.
ErrorDocument 403 /v1.0/errordocuments/403.php
ErrorDocument 404 /v1.0/errordocuments/404.php
Then make those pages with the relevant information you want to show.

Related

.httacces custom redirectioning

I want to redirect all invalid traffic to my index page.
Invalid traffic in this context means:
-Case 1. Nonexistent directories
-Case 2. Nonexistent files
-Case 3. Existing route but only directory: Like in example "validroute/images" or "validroute/images/"
Actually my working code is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [R,L,QSA]
Options -Indexes
For cases 1 and 2 the conditions and the RewriteRule are redirecting correctly to my index.php
For the third case Im using the "Options -indexes" which avoids user to browse my directory and displays the forbidden error. I want to redirect this last case also to the index.php instead.
The nearest approach i got based on redirect 403 error using .htaccess was this
ErrorDocument 403 /index.php
Instead of redirecting ALL forbidden (403) when trying to display a valid "folder" (and ONLY in this case) i seek to redirect to index.
By setting the same redirects i expect the user not to be able to see my directories even if he tries to manually input different routes to see if the directory exists or not. Actually he has the hint because some are "forbidden" and others redirects to index.
This is NOT a duplicate of these because given answers didn't fit me or the issues are not what I intend:
.htaccess redirect - Options -Indexes
.htaccess option -indexes redirect?
.htaccess to redirect everything to index.php, but keep a copy of the website in a subdirectory
Maybe is not possible at all or maybe there is another command or maybe a proper regex combined to RewriteRule to fix the selection.
There is a layer above for the server at which i am not able to access or to see in any way, but as i was handling other kind of permissions (folder accesses by password) i noticed that this had its own way of working without allowing developer to modify this.
So i guess there is no way to override such from a higher lvl .htaccess without direct access to server.
I was using Plesk Obsidian.
So if you dont have full control on the server your configurations might generate some conflicts.

Redirect broken 404 pages with .htaccess and regular expressions

I have been unpublishing old pages on my website. To avoid 404 pages, I'd like to redirect these pages to a generic page.
So for example this page:
https://www.portal-gestao.com/artigos/7380-7-t%C3%A1cticas-de-sobreviv%C3%AAncia-%C3%A0-crise-nas-vendas.html
Should redirect to:
https://www.portal-gestao.com/artigos/
I'm not very skilled with .htaccess or regular expressions, I've bee trying to redirect the pages with:
RewriteRule ^artigos/(.*)$ /artigos/$1 [R=301,L]
But something isn't working, can anyone help?
Late information... the site uses a Joomla CMS. See the UPDATE below.
To redirect requests for physical files that no longer exist you need to actually check that the file no longer exists, otherwise it will indeed "redirect everything" (as mentioned in comments).
For example, to redirect any requests of the form /artigos/<something>, that do not map to physical files, to /artigos/ you can do the following:
RewriteEngine On
REwriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(artigos/). /$1 [R=302,L]
The $1 backreference simply prevents you having to repeat the directory name.
The first condition that checks against the REDIRECT_STATUS environment is intended to ensure that only direct requests are redirected. This is probably only required if you are still on Apache 2.2 (as opposed to 2.4) since mod_dir will execute first, rewriting the redirected request to index.php (if it exists) and causing a rewrite loop. On Apache 2.4, mod_dir executes later.
Test with a 302 and only change to a 301 when you are sure it's working OK - to prevent caching issues.
You will need to clear your browser cache before testing.
However, a 404 would generally be a better response. The search engines will likely see the redirect to a common root as a soft-404 and users are more likely to be "confused" when they don't see the information they requested.
RewriteRule ^artigos/(.*)$ /artigos/$1 [R=301,L]
By itself, this would result in a redirect loop, as it simply redirects to itself.
UPDATE: it's not a file, it is an article in a Joomla CMS
If valid URLs do not map to physical files then you can't do this in .htaccess. In your case, a valid URL is determined by the Joomla CMS (as stored in the Joomla database). .htaccess is processed at the very start of the request, before control passes to PHP/Joomla. Directives in .htaccess can only look at the HTTP request and the physical filesystem.
Joomla uses a front-controller pattern. All URLs, that do not map to physical files (to exclude static resources like CSS, JS and images), are internally rewritten to index.php (the "front-controller"), this effectively "routes" the URL and decides what content should be returned.
What you are asking could only be done on a static website where URLs map to physical files on the file system.
You need to perform this redirect in Joomla itself, when Joomla has determined that the requested URL does not exist. (This is actually more efficient anyway as you only need to execute your code after a 404 has been determined, rather than on every single request, as it would be if you used .htaccess.)

Getting rid of unwanted subdir suffix in URL with .htaccess

I have a URL https://example.com/portfolio/portfolio/page/2/ which I need to redirect to https://example.com/contact/
My attempts are:
RewriteCond %{REQUEST_URI} ^/portfolio/portfolio/page/2/ [NC]
RewriteRule ^/?/page/2/(.*)$ /contact/$1 [R,L]
OR
Redirect 301 /portfolio/portfolio/page/2/ /contact/
Both fail and redirect to https://example.com/contact/page/2/ instead of just https://example.com/contact/
Any help would be appreciated.
Based on the additional information you gave in the comments to the question this is the simple rewriting rule you want:
RewriteEngine on
Rewrite /?portfolio/portfolio/page/2/?$ /contact [END]
In case you want to change the URL visible in the browser, then you need to implement an external redirection:
RewriteEngine on
Rewrite /?portfolio/portfolio/page/2/?$ /contact [R=301]
In case this results in an internal server error (http status 500) then chances are that you operate a very old version of the apache http server. Try using the [L] flag instead of the [END] then. You will see a corresponding entry in your http servers error log file in that case.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Htaccess redirect with subdirectories and dynamic querystring

I have a Magento site installed in subfolder like http-host/subdir1/subdir2/siterootdir.
Now I want to shorten a rather long URL of my brands page like below:
Request URL:
http://hostname/subdir1/subdir2/siterootdir/brands/brandname.html
to
Original URL:
http://hostname/subdir1/subdir2/siterootdir/brands/index/brandsproduct/brand/brandname
with htaccess redirects with dynamic querystring.
I have tried many examples from google searching, but I keep getting 404 not found page when I run my required request URL (as above).
Also I am not so good at regex & rewrite rules/conditions, so can anybody provide solution for above issue & also explain details behind it?
Note that brandname is dynamic, of course.
I took a different approach for solving these, as I needed to rewrite only brands specific urls, so it wasn't entirely related to htaccess scenario I guess, so I changed approach little bit & did rewrite programmatically into Core URL Rewrites("custom" type) whenever any brand was saved from admin, also I didn't want the urls into browser to change, which is provided by magento into URL Rewrites.
Even though if somebody finds solution the "htaccess" way then let me know, I am still open for that, as I won't have to save every brand for creating it's url rewrite.
This .htaccess file will redirect http://hostname/subdir1/subdir2/siterootdir/brands/brandname.html to http://hostname/subdir1/subdir2/siterootdir/brands/index/brandsproduct/brand/brandname:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} hostname$ [NC]
RewriteCond %{HTTP_HOST} !index
RewriteRule ^(.*)\.html$ http://hostname/subdir1/subdir2/siterootdir/brands/index/brandsproduct/brand/$1 [R=302,L]
Put this at the http://hostname/subdir1/subdir2/siterootdir/brands/ location
The \.html strips the html extention from the URL.
The R=302 marks the redirect as temporary. Use 301 for Moved Permanently.

how to generate an internal redirect of a google gclid url which is causing a redirect loop?

for
joomla 1.5.26 website
running virtuemart 1
and SH404SEF 2.5.0.1040 enabled.
"Google Ads" urls returned to the site from a google campaign are causing a redirect loop:
here is an example:
http://www.brand-it.co.il/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%9B%D7%9C%D7%99-%D7%9B%D7%AA%D7%99%D7%91%D7%94/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%A2%D7%98%D7%99%D7%9D.html/?gclid=CKyLgNSjxbkCFbHKtAoaaaaaa
i upgraded the SH404SEF and isolated the problem to a trailing forward slash after the .html part of the url
so instead of this part .html/?gclid=
i remove the slash like this .html?gclid=
and it works fine:
http://www.brand-it.co.il/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%9B%D7%9C%D7%99-%D7%9B%D7%AA%D7%99%D7%91%D7%94/%D7%94%D7%93%D7%A4%D7%A1%D7%94-%D7%A2%D7%9C-%D7%A2%D7%98%D7%99%D7%9D.html?gclid=CKyLgNSjxbkCFbHKtAoaaaaaa
so my question is: how to keep the original url indexed and create an internal redirect from the non-working url to the working one ? using .htaccess ?
thanks
You can try this rule:
RewriteEngine On
RewriteRule ^(.+?\.html)/$ /$1 [L,NC,NE,R=301]