Redirect 404 Sub Directories to the equivalent sub-domain
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, 404, , , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :We're moving thousands of sub directories, to a sub domain. The exact same slug, but its a sub domain now. All of these sub directories are going to cause a 404.
Using htaccess, how can I take the slug of the 404 at domain.com/slug/ and redirect to slug.domain.com?
I appreciate anyone's time!
Using mod_alias's RedirectMatch
RedirectMatch permanent "^/?([a-z0-9-]+)/?$" "https://$1.example.com/"
Using mod_rewrite:
RewriteRule "^/?([a-z0-9-]+)/?$" "https://$1.example.com/" [R=301,L]
This redirects only directories that are made up of the characters that are safe for subdomains (numbers, letters, and dashes -- [a-z0-9-]). This doesn't check to see if it starts or ends with a dash, (not allowed as a subdomain), so this could be improved a bit. To make sure it doesn't start or end with a dash, you could instead use [a-z0-9]+(-+[a-z0-9]+)* inside the parenthesis.
The slashes are both optional (the ? after each slash). That means it will redirect both /slug and /slug/. The first slash is optional just because it makes the mod_rewrite rule more portable. It will work either in .htaccess or in your Apache .conf file.
The ^ and $ on the rules are "starts with" and "ends with" respectively. That is so the rules don't match partial URL paths.
The parenthesis are used for grouping. The $1 is the text that is pulled from inside the (first set of) parenthesis.
These assume that the subdomains are handled in a different directory and that there are no exceptions. If you do have exceptions you could add them as conditions to the rewrite rule. For example you might want to make exceptions for any actual files that exist. Another exception might be so that they only work on your main host name don't try to re-redirect slugs on the subdomains. Like this:
RewriteCond "%HTTP_HOST" "www.example.com" [NC]
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-l
RewriteRule "^/?([a-z0-9-]+)/?$" "https://$1.example.com/" [R=301,L]
You can do something like the following using mod_rewrite at the top of the root .htaccess file on the main domain:
RewriteEngine On
RewriteCond %DOCUMENT_ROOT/$1 !-d
RewriteRule ^([^./]+)/$ https://$1.example.com/ [R=302,L]
The RewriteRule pattern ^([^./]+)/$ matches just the first (and only) path segment, excluding files (since the pattern excludes dots). The mandatory trailing slash is excluded from the capturing subpattern. Modifying this to only match a valid subdomain pattern, as in @Stephen's answer, would be advisable.
The preceding condition (RewriteCond directive) ensures that the request does not map to an existing directory. (I'm assuming you have valid directories that need to still function.)
$1 is a backreference to the captured path segment from the URL-path, excluding the first and trailing slash, ie. slug in your example.
Note that this is a temporary (302) redirect. Only change it to a permanent (301) - if that is the intention - once you have confirmed that it works OK. This is to avoid any caching issues.
Examples:
Applying the above directives...
A. Request example.com/slug/.
slug/matches theRewriteRulepattern^([^./]+)/$/slugdoes not exist as a directory on the filesystem.- Redirected to
https://slug.example.com/. SUCCESS
B. Request example.com/directory/.
directory/matches theRewriteRulepattern^([^./]+)/$/directoryexists as a directory on the filesystem. FAIL
C. Request example.com/directory/something.
directory/somethingdoes not match theRewriteRulepattern^([^./]+)/$. FAIL
D. Request example.com/file.html.
file.htmldoes not match theRewriteRulepattern^([^./]+)/$. FAIL
E. Request example.com/slug (no trailing slash).
slugdoes not match theRewriteRulepattern^([^./]+)/$. FAIL
Comments
Post a Comment