I'm looking to centralise the resources of a number of small websites in one hosting account.
The main resources for each site will be in one core folder, however each site should be able to overide the core if needed.
So I'm trying to get htaccess redirection to first look in the local folder for requested files, then fall back to the resources folder if the file is not present.... I'm tying myself in knots!
I know how to do basic redirects and have been reading through various tuts, but think I'm getting the syntax wrong. I was also looking at the fallbackresource directive, but I don't think this is meant for this scenario.
I tried something along these lines but it didn't seem to work, and obviously it is only aimed at the img folder - I'm guessing I need some wildcards in there to cover all bases:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^img/(.*) http://domain.com/resources/img/$1 [R=301,L]
</IfModule>
Any pointers in the right direction would be very much appreciated! This is a very basic illustration of my intended structure:
.htaccess
|
|-/Resources
|-/img
|-img1.jpg
|-/styles
|-style.css
|-/cms
|-core.php
|
|-/Site1
|-index.php
|-page2.php
|-/img
|-/styles
|-/cms
|
|-/Site2
|-index.php
|-page2.php
|-/img
|-/styles
|-style.css [local override file]
|-/cms
Related
First of all, this question has been asked a few times on stack, however, none of the answers seem to work for me.
I have a website which has a "pages" folder in the root, I want to store all of my website pages in there.
Here's my structure
/pages/folder/folder2/index.php
I want to make it so the link displays:
https://wwww.website.com/folder/folder2/index.php
Removing the "/pages/" part of the URL, when I try all of the answers suggested previously, I get a 404 error.
Here is the htaccess I'm using
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^pages(/.*|)$ $1 [L,NC]
</IfModule>
and i also tried:
RewriteEngine On
RewriteRule ^pages/(.*)$ /$1 [L,R=301]
This htaccess is in the root. I can't seem to get it working, can anyone offer any suggestions? Thank you!
Your second attempt looks fine, though it can be imporoved a bit:
RewriteEngine On
RewriteRule ^/?pages/(.*)$ /$1 [R=301]
That rule should work inside the http servers host configuration or in some dynamic configuration file (".htaccess" style file) if the http server's rewriting module is loaded. You definitely should prefer the first option, but if you really need to use a dynamic configuration file then take care that the interpretation of such files is configured at all and that the file is placed in your hosts's DOCUMENT_ROOT folder and is readable for the http server process.
If that does not work then you need to start debugging. In this case you will start by monitoring your http server's error log file while making test requests. If no hint appears in there you need to enable rewrite logging which allows you to learn what exactly is going on inside the rewriting engine. See the official dpcumentation for the apache rewriting module for that. As typcial for OpenSource software it is of excellent quality and comes with great examples.
How can I convert URL from:
http://example.com/site/delete-page.php?site=first&page=second
To:
http://example.com/site/first/delete-page/second
Thank You!
This is the normal rule for that specific task:
RewriteEngine on
RewriteRule ^/site/([^/]+)/delete-page/(.+)$ /site/delete-page.php?site=$1&page=$2 [L]
To use the same rule inside a .htaccess style file you have to modify it slightly: remove the leading slash (/) from pattern and target:
RewriteEngine on
RewriteRule ^site/([^/]+)/delete-page/(.+)$ site/delete-page.php?site=$1&page=$2 [L]
This assumes that the .htaccess style file is located inside the folder holding the site folder. You can also place the file inside that site folder, then obviously you have to remove the site/ part from the rules pattern and target. Also you have to take care that the interpretation of .htaccess style files is enabled at all for that host and location.
In general you should always prefer the first version and place such rules inside your http servers host configuration. .htaccess style files are notoriously error prone, hard to debug and they really slow the server down. They are only offered as a last option for situations where you do not have access to the server configuration. For example when using a really cheap shared web hoster service...
I've got a problem converting a rewrite rule from Apache to nginx. In Apache I used a .htaccess file containing this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)-single.*php$ single.php?id=$1 [L]
</IfModule>
to rewrite URL for a gallery (displaying a single image). This .htaccess file is located in the specific folders but the tree can be like this:
/galleries/album1
/galleries/set1/album2
/galleries/set2/set3/album3
And the Folder name "Album" can vary (europe, horses, whatever)
The rule itself I would rewrite like this
rewrite ^/(.*)-single.*php$ /single.php?id=$1 break;
but how can I apply this rule only if for the specific folders? Thought about looking for a specific file as a condition (which only exists in the folders I would like to apply the rule) but I can't figure it out.
Please help!
Edit:
Trying to start with a simple example:
If someone is browsing a gallery he can use a link which opens the image. This link can look like this (autogenerated)
...rvserver.net/galleries/album-set/album-2/DSC01154-single.php
This link for social media use but it only works when it Looks like this
...rvserver.net/galleries/album-set/album-2/single.php?id=DSC01154
so you see the only thing changed is the last part of the URL. The front part (until .../galleries/) is always the same and the last part (XXX-single.php) too. Maybe this helps
I have a small personal project I'm working on, and before I get too deep into it, I'd like to get some opinions here on the best practices regarding the set up and URL scheme... and how I might set up the .htaccess rewrites for the following. My questions are:
Is the below set up the best way to manage user information in a small user profile setup?
I have worked out how to get the subdomain working for the rewrite username.domain.com with wildcard subdomains and httpd.conf file set up... but I am stuck on the rest of the scheme seen below. Basically, a user profile will always result in username.domain.com and then appended with the various pages within their account (photos, videos, notes etc). How would I set up the .htaccess rewrites to accommodate this? I really appreciate any advice here. I have done a ton of research here on stackoverflow, and on other sites, but I can't find a decent explanation to achieve this.
Thanks for any help.
www.domain.com/profile.php?u=username --> username.domain.com
www.domain.com/photos.php?u=username --> username.domain.com/photos
www.domain.com/photos.php?u=username&a=album --> username.domain.com/photos/album
www.domain.com/photos.php?u=username&a=album1&p=photoid --> username.domain.com/photos/album1/photoid
www.domain.com/settings.php?u=username --> username.domain.com/settings
etc
Your proposed setup looks fine to me. Here are some rules for .htaccess (make sure you have mod_rewrite enabled and AllowOverride All set in your httpd.conf):
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ http://www.domain.com/profile.php?u=%1 [L,QSA]
RewriteRule ^photos/?$ http://www.domain.com/photos.php?u=%1
RewriteRule ^photos/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1
RewriteRule ^photos/([^/]+)/([^/]+)/?$ http://www.domain.com/photos.php?u=%1&a=$1&p=$2
RewriteRule ^settings/?$ http://www.domain.com/settings.php?u=%1
Wich server do you use? And first of all use rewrites in the config file of your server, not htaccess, htaccess slows the server down.
Edit: Iam not shure about the speed, but as far as i remember htaccess slows down apache, i dont know about how much. Iam sry you have to google that :)
Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>