1
votes

Looking for some help from an expert on writing some .htaccess rewrite rules. Here is what I'm trying to accomplish:

#) User requests                    Displayed to User As            Actual Request to Server
1) www.example.com*                 example.com*                    example.com*
2) example.com                      example.com                     example.com/index.php
3) example.com/index.php            example.com                     example.com/index.php
4) example.com/mypage               example.com/mypage              example.com/index.php?p=mypage
5) m.example.com                    m.example.com                   example.com/mobile.php
6) m.example.com/index.php          m.example.com                   example.com/mobile.php
7) m.example.com/mobile.php         m.example.com                   example.com/mobile.php
8) m.example.com/mypage             m.example.com/mypage            example.com/mobile.php?p=mypage

Here is what I have working so far. It takes care of lines 1, 3 and 4. As soon as I start adding the mobile stuff, I get lost.

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]

RewriteRule ^([a-z\-]+)$ /index.php?p=$1 [L]

I've spent too much time on trying to figure this out, and I just keep running into dead ends. I keep getting parts of it to work, but then other parts break. Any help would be appreciated.

Thanks!

1

1 Answers

0
votes

I'd imagine you have to add the mobile rules first so that you don't accidentally trigger your other rules first.

RewriteEngine On

# 6,7
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteRule ^\/(index|mobile)\.php$ http://m.example.com/ [R=301,L]

# 5,8
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteRule ^([a-z\-]+)$ /mobile.php?p=$1 [L]

# 1
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) http://example.com$1 [R=301,L]

# 3 (and example.com/mobile.php)
RewriteRule ^\/(index|mobile)\.php$ http://example.com/ [R=301,L]

# 2,4
RewriteRule ^\/([a-z\-]+)$ /index.php?p=$1 [L]