mod_rewrite - Fixing absolute paths - linux

I have a site in a staging environment that sits on an IP in a virtual directory, example: http://0.0.0.0/~drupal. The problem is, I've set up all of the image, css, and js URIs using absolute pathing, example: /path-to-image/img.jpg.
Since the site is sitting in a directory though, /path-to-image/img.jpg points to http://0.0.0.0/path-to-image/img.jpg
instead of http://0.0.0.0/~drupal/path-to-image/img.jpg.
I believe the solution lies in utilizing mod_rewrite, but have been unable to implement a successful solution.
Sidenote: I think it's also possible to modify my host file on my local computer when viewing the site to get a desired effect. However, this won't be an option since there will be many parties viewing the site as it undergoes changes.

You could try adding this to the DOCUMENT ROOT, meaning the directory that http://0.0.0.0/ accesses:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/~drupal%{REQUEST_URI} -f
RewriteRule ^(.*)$ /~drupal/$1 [L]

Related

Rewrite rule behaving differently between environments

I have two environments, both using Bitnami installations (if that makes a difference). My local environment is WAMP and works correctly. The second is on EC2 using LAMP and does not work correctly. I've done some basic checking and things seem to be setup the same but still give different results.
RewriteEngine On
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]
The .htaccess page is the same, as are the folder configurations. I have all files in the root folder.
-root
-index.php
-study.php
If I go to //localhost/study/ it correctly redirects to index.php. However on my EC2 instance, it instead goes to study.php. Even if I go to //ec2/study/random/2021/bleh/ it will redirect to study.php. Going to //ec2/random/ will however go correctly to index.php since I do not have a "random" folder.
I've run out of ideas of where to look for configurations differences (I even set "AllowOverride All" in the httpd.conf file and tried -MultiViews). Any help/ideas would be greatly appreciated!

Mod Rewrite tweak to ignore asset directories

I'm configuring Expression Engine on Windows using IIS and have ISAPI v3 Rewrite installed.
It's partly working. The main site and subpages work but needs to be modified because some web page assets are stored in similarly named directories.
The recommended Rewrite provided by Ellislabs is this and I've modified it a little to work with our Win 2012 IIS 8 server:
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) abc/$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ abc/index.php?/$1 [L]
For example, the URL http://oursite.example.com/abc works.
Subpages are mostly working and I suspect this applies to any page really but I'm noticing it on subpages. It removes index.php and mostly loads, such as http://oursite.example.com/abc/subdept/page/
However our developer has some assets kept in a server directory named /uploads/abc/ so if a page refers to this directory, it fails to load those assets because it contains the same name, "abc".
Thus, what is the best way to handle this?
I'm guessing I can either tell it to ignore "uploads/cls" or correct the current Rewrite so that it only looks at the first "abc". I'd like for the solution to cover most similar issues that would arise so I don't have to keep modifying it. We have 12 sites and I'll have to apply the solution to each one.
Everything I've tried hasn't worked.
Also, I thought !-f and !-d would tell it to ignore it if the file or directory existed and that doesn't seem to be working as I'd expect here because these images in /uploads/abc/ do exists.
Thanks!
--
Additionally just trying to get it to work at all, I tried adding a htaccess file with "RewriteEngine Off" in the /uploads/abc/ directory and that failed to fix it.
I also tried to add this after each comment and it fails to fix it:
RewriteCond %{REQUEST_URI} !^/excluded-folder/.*$
Seeing how both of the above attempts fail to fix it, I'm wondering if there could be something else going on. Any ideas?
My rewrite was fine. The problem turned out to be code within an Expression Engine template that the in house developer created. They updated the code and the images are loading fine now.

.htaccess cascading conditions

I'm trying to figure out how to manage potentially conflicting conditions in .htaccess
My setup is the following:
- I have a CMS running on a server that can be accessed through myCMSdomain.com where myCMSdomain.com would be CMS home page and myCMSdomain.com/admin would be the admin interface.
- Sites using this CMS should be pointing to myCMSdomain.com/sites/index.php
- Images for all sites are available somewhere behind myCMSdomain.com/admin/images/sitename/...
So here is how I tried to tackle this problem:
RewriteCond %{HTTP_HOST} !^(www.)?myCMSdomain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/index.php
With this, all incoming requests coming from other domain names are treated correctly by the index.php file but as images are hosted somewhere else, I'd like to use a rule saying that images should be fetched somewhere else like this:
Visible url format for images is: images-[sitename]/[image_path]
Real location of the images is: admin/site/[sitename]/[image_path]
The following rule works but not in combination with the first rule
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ admin/site/$1/images/$2
Images end up calling index.php instead of using the rule I defined for them.
I have tried to excluse the image directory from the conditions but it doesn't work either:
RewriteCond %{REQUEST_URI} !(images-([a-zA-Z0-9]+)/(.*))
I might have similar issues in the future with other exception so I was wondering if there was a way to handle this.
Thanks!
Laurent
UPDATE 1:
If I use the following rule on top of all other
it works only if I'm using myCMSdomain.com domain name
if I use any other domain like anotherdomain.com, the rule leads to a http 500
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ manager/site/$1/images/$2
So http://www.myCMSdomain.com/images-test/test.jpg leads me to the correct image
But http://www.anotherdomain.com/images-test/test.jpg leads me to a 500 http error code while this domain is pointing correctly to sites/index.php
UPDATE 2:
On Justin's request, here is a view on the physical directory structure on the server
/admin/
/admin/site
/admin/site/site_name/
/admin/site/site_name/images/
/sites/
/sites/js
/sites/css
You can rearrange your conditions logic.
# if www.myCMSdomain.com or myCMSdomain.com -> do nothing
RewriteCond %{HTTP_HOST} ^(?:www\.)?myCMSdomain\.com$ [NC]
RewriteRule ^ - [L]
# if we reach here, this means it's a subdomain/another domain
# images rule
RewriteRule ^images-([^/]+)/(.+)$ /admin/site/$1/images/$2 [L]
# not a file/directory -> sites/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /sites/index.php [L]
It is also possible the way you did but it would be longer to write.
Also, keep in mind that conditions (RewriteCond) are non-cumulative: they are for next rule (RewriteRule) only.
I've been struggling with this issue for some time now but at last I think I have found the issue. It looks like there was something wrong in the domain name configuration, I don't know exactly what but once I had re-saved the mapping of all domains, the htaccess worked the way it should.
In the end I have used Justin's proposition, it looks more future proof than mine.
Thanks for your help and happy new year to all
Laurent

Move website to subdirectory

I'm working on a new version of a website and I'd like to move the files/folders of the old one to a subdirectory 'old'. Is there a way to keep all links (css, references to "includes" folder and files, etc.) working using a redirect rule, instead of having to manually edit all php files?
Thanks!
You can move all the old files to a directory old and then have a rewrite rule in root .htaccess like this:
RewriteEngine On
RewriteBase /
# if file exists inside old then append /old/ in front of it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/old/$1 -f [NC]
RewriteRule ^(.+?)/?$ old/$1 [L]
No, not as far as i know.
If you use phpstorm you can refactor references easily over all pages, this might fix your problem.
For future projects, it's good practice to use a variable $absolutePath = "C:\...\"
and save the path to the main directory in it, and use that variable in all files.
to link to an image you would do as following:
$imagepath = $absolutepath."subdirectory\image.jpg";
If you move your website you only have to change the one variable.
This is also something you should think about with other frequent used variables like mysql information.

RewriteRule wont work when link contains dash (on loaclhost xampp)

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This works for links like:
working link (will link to subpagewithoutdash.html)
But it doesn't work for links with dash:
not working link (should link to subpage-with-dash.html)
Any idea of how to fix it? I am sitting on a Windows computer with Xampp installed (localhost).
I can immediately think of two situations would cause this rule not to be applied:
subpage-with-dash is the name of a directory in the site root. This would trip the RewriteCond and cause the rule to be skipped.
The URL has a period in it. The pattern in the RewriteRule — ^([^\.]+)$) — excludes URLs that contain any periods. So, if subpage-with-dash is actually subpage-with-dash.something, the rule wouldn't apply.

Resources