質問記載の.htaccess
で期待通りの動作してますよ。
確認時はPCのブラウザのキャッシュをクリアして確認してますか?
# cat -n .htaccess
1 RewriteEngine on
2 RewriteBase /
3
4 RewriteCond %{REQUEST_URI} !/sp/
5 RewriteCond %{HTTP_USER_AGENT} (iPod|iPhone|Android.*Mobile|Windows.\ Phone)
6 RewriteCond %{REQUEST_URI} !(^/img/)
7 RewriteCond %{REQUEST_URI} !.css$
8 RewriteCond %{REQUEST_URI} !.js$
9 RewriteRule ^(.*)$ sp/$1 [R=301,L]
10
11 RewriteCond %{REQUEST_URI} /sp/
12 RewriteCond %{HTTP_USER_AGENT} !(iPod|iPhone|Android.*Mobile|Windows.\ Phone)
13 RewriteCond %{REQUEST_URI} !(^/img/)
14 RewriteCond %{REQUEST_URI} !.css$
15 RewriteCond %{REQUEST_URI} !.js$
16 RewriteRule ^sp/(.*)$ $1 [R=301,L]
root:/var/www/html# curl --head -H 'User-Agent: iPhone' http://localhost/
HTTP/1.1 301 Moved Permanently
Date: Tue, 15 Oct 2019 02:56:00 GMT
Server: Apache/2.4.29 (Ubuntu)
Location: http://localhost/sp/
Content-Type: text/html; charset=iso-8859-1
root:/var/www/html# curl --head -H 'User-Agent: iPhone' http://localhost/sp/
HTTP/1.1 200 OK
Date: Tue, 15 Oct 2019 02:56:28 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Type: text/html;charset=UTF-8
root:/var/www/html# curl --head -H 'User-Agent: hogege' http://localhost/
HTTP/1.1 200 OK
Date: Tue, 15 Oct 2019 02:56:13 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Wed, 05 Jun 2019 07:30:22 GMT
ETag: "2aa6-58a8e91fe5bcb"
Accept-Ranges: bytes
Content-Length: 10918
Vary: Accept-Encoding
Content-Type: text/html
root:/var/www/html# curl --head -H 'User-Agent: hogege' http://localhost/sp/
HTTP/1.1 301 Moved Permanently
Date: Tue, 15 Oct 2019 02:56:20 GMT
Server: Apache/2.4.29 (Ubuntu)
Location: http://localhost/
Content-Type: text/html; charset=iso-8859-1