Move website to subdirectory - .htaccess

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.

Related

.htaccess sub-folders to read parent folder file while maintaining 'pretty' url

This will have been asked and will have an answer. I cannot remember what this feature is and attempts to once again learn it have come up dry.
I'm looking to place a .htaccess file within a folder where the file to read is located, anything beyond that will read that file.
Example:
www.url.com/articles/animals
www.url.com/articles/animals/safari
www.url.com/articles/animals/safari/lions...
Would display the file at
www.url.com/articles/file-to-read.php
So you can then grab the animals, safari, lions to display the necessary content*, maintaining the 'pretty' URL.
*To clarify, I'm not asking for the code to obtain the path parts, this is just an example usage of said parts.
I think you are talking about the pattern matching and rewriting to the actual file on server, something like below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^articles/(.*)$ articles/$1.php [L]
Or
RewriteRule ^articles/(.*)$ articles/filetoread.php [L]

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.

mod_rewrite - Fixing absolute paths

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]

Localization parameter as a subdirectory in the url. How to take into account the redirection in html?

The idea is to redirect http://thesite.com/fr/page1.htm to http://thesize/page1.htm?lang=fr
I could find around how to set .htaccess and redirection is working with this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/([^/]+)$ /$1?lang=fr [L,QSA]
But the problem I can not find a solution is from the redirected page itself. The paths to folders seem to be relative to the subdirectory /fr as to html it looks like it is in the subdirectory fr. What would be a clean solution for this? a in the on each page?
Thanks!
I'd say using an absolute path is the way to go in this case.
Adding <base> leads to other problems, because it changes the relative root for the whole page
Using relative paths is too tiresome if you have multiple possible levels of sub-directories.

.htaccess, proper rewriting of directory and file with same name

As of now my website has a few static pages, one of which is /portfolio. Among other things, my htaccess hides the .html extension. I'd like to add a portfolio directory, but I do not want to move my existing portfolio page into the portfolio directory as the default index file. My /portfolio page is one of my Google sitelinks and I am afraid if it is moved or if the url changes in someway, Google will consider it to be a brand new page.
My problem is once I add the /portfolio/ directory, whenever I try to visit the original /portfolio page, a trailing slash is automatically added and it links to the directory itself.
I've tried countless options, one being a rewrite of /portfolio/ to /portfolio, however this creates an infinite loop. I also tried "DirectorySlash Off" but that only removed the trailing slash while being inside the directory, it didn't revert access to the original /portfolio page.
Ultimately, I would like to keep my /portfolio page as-is, linking to pages inside the directory like so /portfolio/example and if either /portfolio or /portfolio/ is accessed it will result in showing the same page which is outside of the directory without Google thinking it is duplicate content.
A similar question exists here:
.htaccess rewriting url to page or directory though this still resulted in an infinite loop for me for some reason, I'm guess it has something to do with the hidden extensions.
Here's my htaccess-
RewriteEngine On
# HTML to PHP
RemoveHandler .html .htm
AddType application/x-httpd-php .htm .html
# Hide extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Force WWW
RewriteCond %{HTTP_HOST} ^mydomain\.net
RewriteRule ^(.*)$ http://www.mydomain.net/$1 [R=301,L]
# Blog Subdomain
RewriteCond %{HTTP_HOST} ^blog.mydomain.net$
RewriteRule ^(.*)$ http://www.mydomain.net/blog/$1 [R=301,L]
I know it's not a great idea having a directory with the same name as a static page, but I really would rather not alter the existing page and lose the Google sitelink, so a clean and proper way to handle this would be a help.
There are two things going "wrong" here, and two ways to fix it.
The first is that apache "figures out" that there is a directory by the name of "portfolio" before the rewrite conditions are applied. That means that the rewrite conditions are receiving "portfolio/" instead of "portfolio".
Second, the "!-d" rule is specifically avoiding the rewrite that you want to make if there is in fact a directory by that name
Solution 1: Manually re-route requests for the portfolio directory to remove the slash.
# Manually re-route portfolio/ requests to portfolio
RewriteCond %{REQUEST_FILENAME} portfolio/$
RewriteRule ^(.*)/$ $1
# Hide extension
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Note the removal of the "!-d" condition.
The downside to this is that you are having to hard-code the "portfolio" edge case directly into the rewrite rules, and will still result in the browser being first redirected to portfolio/
Solution 2: Set DirectorySlash Off and remove directory exists test
# Disable Automatic Directory detection
DirectorySlash Off
# Hide extension
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Setting DirectorySlash Off would fix this issue the best, but may break other parts of your site where you actually want the auto DirectorySlash. Best of Luck, and I hope this helps.
Note when testing solution 2, your browser may remember the redirect of "portfolio" to "portfolio/" and perform the redirect before it even sends the request to the server. Be sure to test in a cache-clear, clean environment for best results.

Resources