.htaccess URL redirection using User Agent

2019-08-09 19:25发布

问题:

I've been trying to find a solution to my situation but none worked. I have the following htaccess file which does the following:

  1. It redirects site.com/m/page to site.com/m/#page

  2. I redirects site.com/d/page to site.com/m/#page

  3. It deletes .php extension and the common_ pattern which I had in front of the pages

The code so far:

Options +FollowSymlinks -MultiViews

RewriteEngine On    
RewriteBase /

RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]

## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]

## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]

I would like the folowing:

a) If some user enters from an iPhone 'site.com/m/...' , continue (do nothing)

b) If some user enters from a desktop ar iPad 'site.com/m/page' to redirect him to 'site.com/d/#page'

I would really appreciate some help.

回答1:

You can use a new rule for this:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_USER_AGENT} !iphone [NC]
RewriteRule ^m/(.+)$ /d/#$1 [R=302,NE,L,NC]

RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]

## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]

## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]