简体   繁体   中英

How to have Different htaccess redirect based on subdirectory requested?

I tried poking around to find a question that addressed this but I didn't know the exact wording, so I couldn't find anything useful.

I have a Wordpress install on my site, following this guide, I was able to move it to a different folder so I now have 2 folders in my main public_html directory.

Im trying upload some p5.js sketches to that second folder so that I can reference them from the Wordpress site. The problem arises when I try and link them in an iframe like this guide suggests. Im referencing with src="website.com/projects/project_name/sketch.html" but unfortunately it gets redirected back to the wordpress installs 404 page.

After some searches, it looks like my main .htaccess file is the problem. My file looks like this (with example/my_subdir changed)

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 
</IfModule>

so my question is, how would I modify this to keep the redirect from "website.com" to the my_subdir/index.php (which is the Wordpress install), but add in a caveat to not redirect website.com/directory2 requests (which is were the projects will be stored)

If the HOST in your code example.com is what you refer to in your question as website.com , and I assume that your code is already working properly except directory2 request. Then try this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_URI} !^/directory2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 
</IfModule>

EDITED There is only 1 line required to be changed for directory2

Down here is my test:

IP: 172.18.0.2

Root Directory: /www/htdocs

Directory tree (html file contents):

-- htdocs/
 | -- my_subdir/
 |  | -- index.html (subdir index.html)
 |  | -- a1.html    (subdir a1.html)
 | 
 | -- directory2/
 |  | -- index.html (dir2 index.html)
 |  | -- b1.html    (dir2 b1.html)
 |
 | -- index.html    (helloworld index.html)
 | -- .htaccess

Down here is my .htaccess file:

 RewriteEngine on
 RewriteBase /
 
 RewriteCond %{HTTP_HOST}        ^(172.18.0.2)$
 RewriteCond %{REQUEST_URI}      !^(/my_subdir/)       [NC]
 RewriteCond %{REQUEST_URI}      !^(/directory2/)      [NC]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$              /my_subdir/$1

 RewriteCond %{HTTP_HOST}        ^(172.18.0.2)$
 RewriteRule ^(/)?$              my_subdir/index.html  [L]

My Tests:

  1. access host
 [user@client tmp]$ wget 172.18.0.2
 Saving to: ‘index.html’
 [user@client tmp]$ cat index.html
 subdir index.html

the request has reached my_subdir/index.html

  1. access 172.18.0.2/my_subdir/a1.html
 [user@client tmp]$ wget 172.18.0.2/my_subdir/a1.html
 Saving to: ‘a1.html’
 [user@client tmp]$ cat a1.html
 subdir a1.html

the request has reached my_subdir/a1.html

  1. access 172.18.0.2/a1.html
 [user@client tmp]$ wget 172.18.0.2/a1.html
 Saving to: ‘a1.html’
 [user@client tmp]$ cat a1.html
 subdir a1.html
 

the request has reached my_subdir/a1.html

  1. access 172.18.0.2/directory2
 [user@client tmp]$ wget 172.18.0.2/directory2
 ...
 HTTP request sent, awaiting response... 301 Moved Permanently
 Location: http://172.18.0.2/directory2/ [following]
 ...
 Saving to: ‘directory2’
 [user@client tmp]$ cat directory2
 dir2 index.html

the request has reached directory2/index.html

  1. access 172.18.0.2/directory2/b1.html
 [user@client tmp]$ wget 172.18.0.2/directory2/b1.html
 Saving to: ‘b1.html’
 [user@client tmp]$ cat b1.html
 dir2 b1.html

the request has reached directory2/b1.html

  1. access 172.18.0.2/testerror
 [user@client tmp]$ wget 172.18.0.2/testerror
 ERROR 404: Not Found.
 [user@server log]$ tail error_log
 AH00128: File does not exist: /www/htdocs/my_subdir/testerror

the request was trying to reach my_subdir/testerror and found nothing

So based on this small test:

  1. any request to my IP host has checked .htaccess rewrite rules
  2. request only host will be rewrite to my_subdir/index.html (test 1)
  3. request sub-url that doesn't start with my_subdir or directory2 will all be rewrite to my_subdir with any follow up string (test 3 & 6)
  4. request to my_subdir with detailed filename will access the file under my_subdir/ (test 2)
  5. request to directory2 will not be redirect to my_subdir , and will access the index or file under directory2 (test 4 & 5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM