Sunday, July 8, 2018

regex - .htaccess ModRewrite when moving web root content into subdirectories?

I'm cleaning up and rearranging the files on my server (basic shared hosting situation) and am digging into .htaccess to do so. I'm moving the content I had in my web root into the subdirectory of /design/v5/ and am trying to use RewriteRule and RedirectMatch to make the following two scenarios work:



#-------------------------
#Redirect root url to subdirectory
# 1. Visits: DOMAIN.TLD/*
# Served: DOMAIN.TLD/design/v5/*
#
# 2. Visits: DOMAIN.TLD/design/v5/*
# Redirected: DOMAIN.TLD/*
#-------------------------


RewriteRule ^/(.*)$ /design/v5/$1 [L,R]
RedirectMatch 301 ^/design/v5/(.*)$ /(.*)$1


As you Apache pros can see, I'm mangling something up in a bad way, but am struggling to see where and how (reading through a lot of reference material from similar questions here and other sites at the moment... us designer/front-end guys can be pretty dense).



There are two things that I'd like to accomplish here in addition:





  1. Maintain relative URLs in my coding

  2. Have the redirect/rewrite cascade down into /design/v5/'s own subdirectories.

  3. If I can group those subdirectories together in brackets like (includes|img|scripts|style) or get everything to work with a wildcard instead of writing out separate sets of ReWrite/Redirect rules, that'd be fantastic.

  4. Not have the rewrite effect other subdirectories (ie DOMAIN.TLD/lawlz/imonaboat.gif would still work as written and not being directed from /design/v5/).



As of now, with fiddling to the basic setup I have, I'm getting one of three outcomes:




  1. A blank index directory list of my web root,


  2. A server error page at my web root,

  3. A browser error page from a mangled version of the web root: http://domain.tld/(.)/(.)/(.)/(.)/(.)/(.)...



Late Night Update:



This SO answer works perfectly on the root level, however doesn't redirect away from the subdirectory [when I navigate to DOMAIN.TLD/design/v5, I instead get a redirect to DOMAIN.TLD/(.*)] and mangles up any non-subdomained directories do to the heavy rewrite rule.



RewriteCond %{THE_REQUEST} ^GET\ /design/comingsoon/
RewriteRule ^design/comingsoon/(.*) /$1 [L,R=301]

RewriteRule !^design/comingsoon/ design/comingsoon%{REQUEST_URI} [L]

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...