Subdomain/subfolder redirect conflict - wordpress

We have a bunch of sites that are subdomains. We are changing the structure of these sites so they are a single site instead of multiple WordPress installs. No problem. Many of the subdomains have authority so we want to add 301 redirects from these subdomains to the new corresponding page. However, the new page parent page URLs conflict with the old subdomain structure.
existing subdomain
sub1.example.com
redirect which is now a landing page.
example.com/sub1
There is a conflict because the subdomain folders need to stay in place to perform the redirects.
I am looking for a way to create a redirect in the .htaccess from the subdomains that does not conflict with the new page URL of the same name?

it is preferred to leave the folders in tact so we can add 301 redirects to the .htaccess files in these folders
This is part of the problem. Any .htaccess file in these subdirectories will override the .htaccess file in the parent directory (the main WP directives I assume). (Strictly speaking it's just the mod_rewrite directives that are overridden. Other modules are inherited.)
Instead, try the following:
(NB: This assumes the subdomain points to a corresponding subdirectory off the main domain's document root. It would simpler if you changed the subdomain, so that it also pointed to the document root of the main site. You would then only need a single (simpler) redirect.)
Delete the .htaccess (and index.php) file(s) in the subdirectory and instead add the appropriate directives in the parent .htaccess file.
Near the top of the root .htaccess file (before any existing WP directives):
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub1|sub2|sub3)\.example\.com
RewriteRule ^[^/]+/(.*) http://example.com/%1/$1 [R=302,L]
This would redirect sub1.example.com/<anything> to http://example.com/sub1/<anything>. The www sub-subdomain on the initial request is optional.
Exactly how you would do this could depend on how many (and what format) subdomains you have. If you have just a handful (you say you have 3) then it would preferable to name these explicitly, as in the above RewriteCond directive.
It is easier to use a 302 (temporary) redirect whilst testing since they aren't cached by the browser. (Ensure the browser cache is cleared before testing.)
You will need to update the main WordPress directives to include an additional rule/condition for when these bare subdirectories are accessed (eg. http://example.com/sub1/). Since /sub1 is a physical directory, WP will not route the request, because the WordPress front-controller specifically ignores existing files and directories.
In the main WP .htaccess file, you have the following directives:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Add the following rule immediately after the above:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(sub1|sub2|sub3)/?$ /index.php [L]
This allows requests for the bare subdirectory (eg. http://example.com/sub1/) to be routed by WordPress.

Related

How to use mod_rewrite in .htaccess to point to folder outside WordPress installation

Problem
I would like to edit the WordPress .htaccess file such that requests to www.mydomain.com/nonwordpress are rewritten to point to a folder outside the WordPress installation.
Details
In Detail: The WordPress installation is on shared hosting and the domain is configured to point to the WordPress installation:
www.mydomain.com -> /absolute_path_on_server/htdocs/wordpress
Now I would like to have a different directory "nonwordpress" outside the "wordpress" folder to be redirected like this:
www.mydomain.com/nonwordpress/ -> /absolute_path_on_server/htdocs/nonwordpress
Note that the other folder is outside the root folder to which the domain points.
What I tried:
I tried modifying the .htaccess file in the folder "wordpress" by adding the following line for rewriting requests to mydomain.com/nonwordpress:
RewriteRule ^/nonwordpress/(.*)$ /absolute_path_on_server/htdocs/nonwordpress/$1
The entire .htaccess file now looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/nonwordpress/(.*)$ /absolute_path_on_server/htdocs/nonwordpress/$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Unfortunately it has no effect, so when I go to mydomain.com/nonwordpress I get a 404 site not found error (from WordPress) instead of the folder contents.
I also tried to achieve the rewriting by replacing the absolute path by a relative path:
RewriteRule ^/nonwordpress/(.*)$ ../nonwordpress/$1
Again, the same error.
Any idea how to do this, if it can be done at all, will be appreciated!
Why this doesn't work the way you expect, see RewriteRule
The Substitution of a rewrite rule is the string that replaces the original URL-path that was matched by Pattern. The Substitution may be a:
file-system path
Designates the location on the file-system of the resource to be delivered to the client. Substitutions are only treated as a file-system path when the rule is configured in server (virtualhost) context and the first component of the path in the substitution exists in the file-system
URL-path
...
So this cannot work, when you put it in a .htaccess file. To have the desired effect, you need to put it in the main configuration wrapped inside a VirtualHost directive.
Although in your case, it would be easier to utilize an Alias
Alias /nonwordpress/ /absolute_path_on_server/htdocs/nonwordpress/
but again, this is also only allowed in
Context: server config, virtual host, directory

Convert .html to -html in file name

I converted my website to WordPress. In the process, in order to satisfy WordPress's rules about permalink formats, I had to change the filenames from ending in .html to ending in -html.
Unfortunately, there are backlinks from other sites that point to the old pages that end in .html. So when someone clicks one of those backlinks, the page cannot be found on the new site.
For this reason, I want to permanently redirect all requests for URIs ending in .html using .htaccess so that they end instead with -html.
So for instance:
https://example.com/file1.html
needs to permanently redirect to
https://example.com/file1-html
Sadly, my code generates 500 errors.
Here's what I wrote based upon what I found in the Apache manual:
<Directory /home/accountname/public_html>
RewriteEngine on
RewriteBase /home/accountname/public_html
RewriteCond $1.html !-f
RewriteRule ^(.*).html$ $1-html [R=301,L]
</Directory>
Can someone help?
<Directory> directive isn't allowed in .htaccess.
Place this rule in your main WP .htaccess:
RewriteEngine on
RewriteRule ^(.+?)\.html$ /$1-html [R=301,L,NE,NC]
Make sure this is first rule below RewriteEngine on line.

wordpress - getting rid of /subdir/ in all my links, css styles off after changing Site Url

In wordpress, I have the wordpress files in a subdirectory, mywp, and originally the site's homepage was example.com/mywp/. I wanted to get of that /mywp/ in all the links, so I did the following:
Copied /mywp/index.php and /mywp/.htaccess FROM /mywp/ and pasted TO my root folder
Updated root's index.php, changed require('./mywp/wp-blog-header.php'); to require('./wp-blog-header.php'); (removed the /mywp/)
Updated the Site Url in wp admin's Settings - General FROM http://example.com/mywp TO http://example.com
After step 3, I visited http://example.com, and, the styles were off in both the site and the admin. I viewed source and all the css references were missing the /mywp/ in their url's.
In case someone asks, contents of my .htaccess file, that now resides in both my root and /mywp/ subdirectory are:
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 5
DirectoryIndex index.cgi index.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mywp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mywp/index.php [L]
</IfModule>
# END WordPress
Do I need to tweak another setting to get the styles right? And, do I have to remember this step if I ever update WordPress version or apply a new theme? Thanks!
Changing the Site URL There's four easy methods to change the Site URL manually. Any of these methods will work and perform much the same function.
I was changing the wrong record in the db! I was updating siteurl to be example.com instead of home.
Fix:
1. set siteurl in wp_options table to be http://example.com/mywp
2. set home in wp_options table to be http://example.com
3. still do the steps I have in the original description of this stackoverflow post

Wordpress installed in root folder, subdomain now not working, GoDaddy host

please forgive me for being a complete beginner at this, I'd rather not have to try to deal with this myself but as GoDaddy support have not replied after 2 days I'm going to have to. I think my problem is the same as the one above, but I'm not 100% sure, so I'm reposting it, I'm not really confident enough to attempt to try the fixes I've seen here so I need someone to give me baby instructions?
Our original website (www.mwpics.com.au) was built in Dreamweaver etc, recently we created a new website in Wordpress, in a subdomain, then migrated it over to the root folder where it is now operating fine. I also moved the files for the old website into another directory which I called 'old', so they're all still there.
The problem is that I have a subdomain set up - which is still showing as set up in the control panel on godaddy the url is www.mwpics.com.au/clients and it is at www.clients.mwpics.com.au. This directory contains loads of other directories, each of which is password protected by .htaccess files and which our clients access directly (not through the site) to download their finished work. The test one and the one for random clients is www.mwpics.com.au/clients/temp - username and password both temp (the usernames are all the same as the directory names).
Since the WP install to the root directory the /clients extension no longer works (it should bring up an information page which is an .html index page in the directory) and the /clients/name extensions no longer works - it goes back to the wp site with a 'not found' error message. Strangely it does bring up the box for the username and password, but when you enter it it just goes back to the 'not found' message.
Someone told me it was the .htaccess file - so as an experiment, I renamed the .htaccess file in the root directory and then copied the .htaccess file from the old root files into the root directory, eureka! It worked - and also the WP site opened to the home page... but bummer - the /pages in the WP site now no longer worked! But at least I know the source of the problem.
So I switched it back and this is the status quo - I have no idea how to fix this, and with everyone back at work tomorrow, clients are going to want to start downloading their stuff...
Can anyone help me? I'm starting to panic a bit
you only have to exclude the clients subfolder in your wordpress .htaccess - see .htaccess & Wordpress: Exclude folder from RewriteRule for a detailed description
this should work:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/clients.*
RewriteCond %{SERVER_NAME} !clients\.yourdomain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
short: it simply skips the wordpress rewrite rule if the reqest uri starts with /clients or the domain name is clients.yourdomain.com
be aware though, that as soon as you update your wordpress permalink settings, this rule will be overwritten by wordpress ...
Try adding the following at the top of your root directory's .htaccess file, before the WP code block:
<IfModule mod_rewrite.c>
# fix rewrite for GoDaddy
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} clients.mwpics.com.au$ [NC,OR] # ignore subdomain
RewriteCond %{REQUEST_FILENAME} -d [NC] # ignore directories
RewriteRule .* - [L]
</IfModule>

Using .htaccess to redirect if file isn't found with Wordpress

I have an issue where we are merging a website with Wordpress. The website has a ton of news stories in a "/news" directory that we need to keep. When we add the CMS at say "/wordpress" we can set the base URL to the root of the directory so when we make a new post in the category "news", the link for it is "/news/this-is-the-post-title", except the REAL location is "/wordpress/news/this-is-the-post-title". So my issue is that we want to keep all news stories at the "/news" URL, and in my head we just need the .htaccess to say "if the requested URL doesn't exist in the /news directory, try /wordpress/news", and of course if Wordpress doesn't find anything, it can 404. Hope this makes sense, thank you!
I don't fully understand how the original news stories are held. But, if they are grabbed with PHP then you should be able to redirect to /wordpress/news/post-title if it doesn't exist.
On the other hand, if the news stories are static files you can check for them with .htaccess and make sure the file exists on the server.
# Goes in the "news" directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news
# These make sure the file or directory doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect to /wordpress/news/whatever-entered-goes-here
RewriteRule ^(.*)$ /wordpress/news/$1 [L]
</IfModule>
Reference: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
If you only use WordPress and static pre-existing directories and files (and not another CMS, next to WordPress), you can probably just follow the directions on giving WordPress its own directory. All WordPress files go to wordpress/, only the index.php and .htaccess are copied back to the root directory. The standard .htaccess from WordPress already has the two RewriteCond rules that check for existing files, so if the request is for news/happy-2010.html and this file already exists, your server will just serve this and not start WordPress.

Resources