Home>
Status

Referring here, I am trying to redirect from PC site to smartphone site using .htaccess.
How to automatically switch between the PC version and the smartphone version

http://www.hoge.com/foo/bar (PC page)
http://www.hoge.com/foo/bar/sp (smartphone page)
As .htaccess in/foo.

.htaccess is described as follows.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond% {HTTP_USER_AGENT} (iPhone | Android. * Mobile | Windows. * Phone) [NC]
RewriteCond% {QUERY_STRING}! Mode = pc
RewriteRule/foo/bar// foo/bar/sp/[R, L]
</IfModule>
Question

When I click on the link to /foo/bar/index.html in /foo/index.html, it is transited to the top page (/index.html) regardless of whether I browse from a PC or a smartphone. .

【Postscript】 /. htaccess was skipped to the top page due to error handling.
Error processing resulted in a 403 error.

Forbidden
You don't have permission to access/foo/bar/on this server.</Code></pre>
<p>In case of PC /foo/bar/index.html<br />
/Foo/bar/sp/index.html<br />
How do I make the transition to?</p>
<p>I am sorry that I do not understand how to write .htaccess and regular expressions, but I am happy if you can answer. Thank you.</p>
 Status (updated on 20/2/6/13: 37) 
<p>What to do if a 403 error occurs when redirecting htaccess</p>
<pre><code>Options + FollowSymLinks


After adding ↑ to the first line of /foo/.htaccess, it is now transited to /foo/bar/index.html regardless of whether browsing from PC or smartphone.

Status (updated from 20/2/6/14: 43)

JavaScript: separate one link into PC URL and smartphone URL >

I was able to make the transition as expected by using JavaScript.
We have solved the problem, but we will continue to solicit answers on how to implement it with .htaccess.
I am glad if you know the answer. .

  • Answer # 1

    When usingRewriteRulein.htaccess,
    It must be specified as a relative path from the path where.htaccessexists.

    #/foo/.htaccess
    RewriteEngine On
    RewriteCond% {HTTP_USER_AGENT} (iPhone | Android. * Mobile | Windows. * Phone) [NC]
    RewriteCond% {QUERY_STRING}! Mode = pc
    RewriteRule ^ bar// foo/bar/sp/[R, L]

    However, in the above case, all the requests under/foo/bar/are redirected to/foo/bar/sp /, so a redirection loop is performed. (Because it also matches requests to/foo/bar/sp /)
    Add the condition "other than/foo/bar/sp /" and add the path after/foo/bar/to the redirect destination.
    (If only index.html file exists in */foo/bar/sp/
    RewriteRule ^ bar// foo/bar/sp/[R, L]is fine. )

    #/foo/.htaccess
    RewriteEngine On
    RewriteCond% {REQUEST_URI}!/Foo/bar/sp/[NC]
    RewriteCond% {HTTP_USER_AGENT} (iPhone | Android. * Mobile | Windows. * Phone) [NC]
    RewriteCond% {QUERY_STRING}! Mode = pc
    RewriteRule ^ bar /(.*)/foo/bar/sp/$1 [R, L]
    If only index.html file exists under #/foo/bar/sp /, the following is also possible.
    #RewriteRule ^ bar// foo/bar/sp/[R, L]