Apache, htaccess, url rewrite

.htaccessapache-2.4mod-rewrite

I have .htaccess file I'm using for testing, here is my file:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-z-]+)/?$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%{SERVER_NAME}/?title=%1 [L]

I'm using Apache/2.4.10 (Win32) from ApacheLounge

I have the following domain and sub domain:

  • example.com
  • m.example.com

Both point to the same ip address, now as I expected from the above .htaccess:

if I access example.com/Article-Name/ the redirection to example.com/?title=Article-Name will be internal, I won't be able to see ?title=Article-Name on my url bar.

Here is the exception:

If I access m.example.com/Article-Name/ the redirection to m.example.com/?title=Article-Name will be external and now I see m.example.com/?title=Article-Name in my url bar.

So I was expecting the same behavior, if anyone have an idea I will be very thankful.

EDIT:

I have found a solution for the problem but I still interesting about the above problem, about the solution is to use:

RewriteRule ^ ?title=%1 [L]

Please notice: this solution is only needed for m.example.com, I really have no idea how this solution is even working, I have posted this solution because it may help finding the cause for above problem, and how this solution is even working.

EDIT 2(the full .htaccess file):

RewriteEngine On


<If "%{HTTP_HOST} = 'avielfadida.ml'">

# The actual condition is really long one so I replaced it for illustration.
RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC]

RewriteRule ^ http://m.%{HTTP_HOST}%{REQUEST_URI} [R,L]

# I have to duplicate the RewriteCond and RewriteRule both inside the <If> and <Else>
RewriteCond %{REQUEST_URI} ^/([a-z-]+)/?$ [NC]

RewriteRule ^ %{REQUEST_SCHEME}://%{HTTP_HOST}/?title=%1 [L]

# The solution(the solution is not required inside this block):
#RewriteRule ^ /?title=%1 [L]
</If>

<ElseIf "%{HTTP_HOST} = 'm.example.com'">

# I have to duplicate the RewriteCond and RewriteRule both inside the <If> and <Else>
RewriteCond %{REQUEST_URI} ^/([a-z-]+)/?$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%{HTTP_HOST}/?title=%1 [L]

# The solution(the solution is required here):
# RewriteRule ^ /?title=%1 [L]

DirectoryIndex /m/index.htm /m/index.php /m/index.html
</ElseIf>

Important note: there is no relation between the full .htaccess file and the problem, the reason for this update is for the small chance I have made a mistake, anyway I have tested the .htaccess file with only the 3 lines from the question.

HERE IS THE HTTPD.CONF FILE

Last update:

RewriteEngine On    

# The actual condition is really long one so I replaced it for illustration.
RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC]

RewriteCond %{HTTP_HOST} !^m\.example\.com$ [NC]
RewriteRule .* http://m.example.com%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_URI} ^/([a-z-]+)/?$ [NC]
RewriteRule .* /?title=%1 [L]


<If "%{HTTP_HOST} = 'm.avielfadida.ml'">
    DirectoryIndex /m/index.htm /m/index.php /m/index.html
</If>

Best Answer

The way you have done with the relative path is correct

RewriteRule ^ ?title=%1 [L]

From Apache documentation

Absolute URL

If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.

m.example.com would be considered as a different host and hence Apache performs an external redirect. So to make sure only an internal rewrite is performed, you need to use the relative path as you've done.

You could also strip down a lot of things from .htaccess file. I think this should do what you are looking for.

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC]
RewriteCond %{HTTP_HOST} !^m\.example\.com$ [NC]
RewriteRule .* http://m.exmaple.com%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_URI} ^/([a-z-]+)/?$ [NC]
RewriteRule .* /?title=%1 [L]
Related Topic