htaccess to skip a folder - .htaccess

I see htaccess questions all over the place but I cant seem to get the thing to do what I want. Im not sure if a redirect or a rewrite is what I need.
Im looking for a way to use a sub folder but make it look there is not a subfolder.
I have a bunch of folders in the root of my domain:
www.mysite.com/folder1
www.mysite.com/folder2
www.mysite.com/folder3
www.mysite.com/folder4
Id like to be able to move folder1 and folder2 to a subfolder but not have that folder visible to the user. For example
www.mysite.com/x/folder1
www.mysite.com/x/folder2
www.mysite.com/folder3
www.mysite.com/folder4
But the user would still see it as it originally was.
It seems like I would have to use a redirect and a rewrite. If a user goes to www.mysite.com/folder1 the will actually be in www.mysite.com/x/folder1 without knowing that they are in that subfolder.
Of course I need to do this without disrupting folder4 and folder5. Ideas? Tips? Tricks? Im not too familiar with htaccess

This is what worked for me, maybe it will help someone else:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1
This is telling the server to ignore the Public folder, so when my URL is actually mysite.com/public/file it shows and responds to mysite.com/file

Related

RewriteEngine - How to ignore requests for folders that already exist

What I would like is if the user enters:
www.mysite.com/test
This would redirect to a page that would search my db for an event called test and if the event exists show the event else redirect to a search page.
I've nearly managed to get this to work - so close yet so far...
This is where I've got to:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ show_events.php?q=$1 [NC,L]
It works in that it redirects to the show_events.php page which searches the db for an exact match and displays the event if it finds one, else it redirects to a search page. I'm very pleased with this.
The problem is when the folder (www.mysite.co.uk/test/) exists already an error page shows saying "this page isn't working". The folder just contains a HTML page that displays the word "hello". If you type www.mysite.co.uk/test/index.html the page displays ok so it's not like it isn't working.
I've concluded the search page works fine but the RewriteEngine code doesn't.
This post looked like it would solve my problem but not quite:
Apache RewriteEngine - Rewrite anything that not exists to index.php
I thought that by adding DPI on the end of the RewriteRule it might solve the problem but it doesn't.
I really don't understand RewriteEngine and am just groping around in the dark using trial and error, can someone help me?
The problem is that your rule also rewrites existent directories to the destination file. You need to put a condition to exclude directories from the rule :
RewriteEngine On
RewriteCond ℅{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ show_events.php?q=$1 [NC,L]
The RewriteCondition will prevent your existent directories from being rewritten to the rule's destination url.
You can simply add a condition to avoid touching existing folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ /show_events.php?q=$1 [NC,L]

.htaccess redirect in folder on domain affects same named folder in subdomain

I recently changed a site I've developed to use a Wildcard SSL certificate but I'm having an issue with .htaccess ReWrites in the domain.com/projects folder affecting the cms.domain.com/projects folder.
In the www.domain.com/projects folder is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^([^\.]+)$ $1 [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ /projects/project.php?id=$1 [L]
When an SEO friendly project url e.g. www.domain.com/projects/project-title gets sent to this folder it is directed to the project.php page where it can be displayed.
This works fine, but I came across a problem in the subdomain cms.domain.com/projects, this folder and all its contents display a 404 error, even though all the files are there. I know this .htaccess file is the issue because I have changed the cms.domain/projects folder name and the sub directories appear and I've also removed the code from the .htaccess file and the cms.domain.com/projects folder appears.
I have tried, many of the suggestions found here and else where adding RewriteCond %{HTTP_HOST} ^www.domain.com$ and denying the cms.domain.com url but nothing works, and the projects folder in the cms remains stubbornly in 404 land. I've been looking at this for several days and have found nothing similar on the Interweb. Any help I can get would be greatly appreciated.
Thank you for all the replies so far.

htaccess rule for any keyword to a physical sub-folder

I am struggeling with something I am trying to do regarding folder. I have a sub-folder called "admin" so I can access the site as mydomain.com/admin.
However I want that if I say mydomain.com/foobar or mydomain.com/28y282djdhjd, then it should always assume it is "admin".
Of course the rule shoud not apply to existing files and folders but it is specifically to the first folder from the domain root.
It's as if I want the admin to be accessed no matter what I call it (as long as it is not a physical folder or file)...
I am not good with htaccess if someone could guide me I would truly appreciate it.
Thank you in advance.
So, you need to:
Check that the request isn't for a file that already exists;
Check that the request isn't for a directory that already exists;
Only redirect the request, if it refers to the first folder-level from the domain root;
If that's all correct, something like this should do it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)\/?$ http://example.com/admin [L]

.htaccess Rewrite not working without slash in the end

I'm with an issue with .htaccess and I hope you can help me out.
Basically I have the following structure:
root/
+ _archive
+ index.html
+ folder1
+ folder2
+ css
+ ...
On archive I have a few folders that I want it to be accessible through the root of my site.
So let's say my site calls rafael.com, so I would have the following archives:
http://www.rafael.com/_archive/folder10
http://www.rafael.com/_archive/folder20
http://www.rafael.com/_archive/folder30
I want them to be accessible from (without _archive):
http://www.rafael.com/folder10
http://www.rafael.com/folder20
http://www.rafael.com/folder30
But also having the folders and css, and images and etc on my root to keep working. Keep in mind that under folder10, folder20, folder30 I also can have their own images and css, and javascript.
Well, I'm trying the following .htaccess
RewriteEngine On
RewriteCond $1 ^(folder10|folder20|folder30)
RewriteRule ^(.*)$ _archive/$1 [L]
and that works fine if I call using http://www.rafael.com/folder30/ (WITH SLASHES IN THE END) my problem is when I try to call http://www.rafael.com/folder30 without the slashes it gets REDIRECT to http://www.rafael.com/_archive/folder30/ .
So can anyone explains to me WHY it's being redirecting it and how do I fix it in order to have http://www.rafael.com/folder30 and http://www.rafael.com/folder30/ working without redirecting it? :)
Thank you in advanced.
Do you have any other rules in place? without an [R=30x] flag it shouldn't visibly redirect the url. Also you need to fix your RewriteCond $1 to something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^folder(10|20|30)
RewriteRule ^(.*)$ /_archive/$1 [NC,L]

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources