TommyDo TommyDo - 2 months ago 9
Apache Configuration Question

Build the rewrite rule with specific path

At first, I sorry to make the simple question but I really didn't know how to build the rule with my special path.

My project path is like:

www.example.com/cate/123456/guide/index.html
www.example.com/cate/123456/guide/001.html
www.example.com/cate/123456/guide/002.html
www.example.com/cate/123456/guide/003.html
www.example.com/cate/123456/guide/004.html
...and more


With 123456 is the category number, it will be changed automatically but the file structure inside will be same.

Now I have the mobile version:

www.example.com/cate/123456/guide/**m_index.html**
www.example.com/cate/123456/guide/m_001.html
www.example.com/cate/123456/guide/m_002.html
www.example.com/cate/123456/guide/m_003.html
www.example.com/cate/123456/guide/m_004.html
...and more


So my question is How can I redirect to m_*.html when access by mobile?

Ex:

index.html -> m_index.html
001.html -> m_001.html
...

Any suggest?

Answer

You can try this rule in your site root .htaccess:

RewriteEngine On

RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC]
RewriteRule ^ - [E=MOBILE:1]

RewriteCond %{ENV:MOBILE} =1
RewriteRule ^(cafe/[\w-]+/guide)/?$ /$1/m_index.html [L,NC,R=301]

RewriteCond %{ENV:MOBILE} =1
RewriteRule ^(cafe/[\w-]+/guide)/(?!m_)([\w-]+\.html)$ /$1/m_$2 [L,NC,R=301]
Comments