Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule doesn't work.

         

seti

4:35 am on Mar 12, 2010 (gmt 0)

10+ Year Member



Hello,
I tested on my apache server following rule to redirect all requests to use https protocol. In this case everything works fine.

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI}




The problem is I don't want redirect to ssl all requests that's why I changed last line of aforementioned rule to following and nothing works.

RewriteRule /(user|admin) https://%{HTTP_HOST}%{REQUEST_URI} [R]


What is the problem? Please, help.

g1smd

7:37 am on Mar 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In what way does it 'not work'?

Note that the [R] should be [R=301,L] and that RewriteRule, when used in .htaccess, cannot see the leading / of URL requests. You should also ^start anchor your pattern.

You must also install another redirect such that if a user asks for folders other than user or admin as HTTPS, they are redirected to HTTP for those requests.

seti

8:32 am on Mar 12, 2010 (gmt 0)

10+ Year Member



Thank you for your reply g1smd.

I tried this code as you said:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
#RewriteRule ^(.*) [%{SERVER_NAME}%{REQUEST_URI}...]
RewriteRule ^/(user|admin) [%{HTTP_HOST}%{REQUEST_URI}...] [R=301,L]
</IfModule>

But it does nothing. For example, when I try to call this url 192.168.0.1/user it is opening in 80 port. Like there was no rule.
Any ideas?

seti

8:39 am on Mar 12, 2010 (gmt 0)

10+ Year Member



By the way, I'm using .htaccess file.

jdMorgan

9:56 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The URL-paths 'seen' by RewriteRule in .htaccess do not start with a slash, as the URL-path to "this" .htaccess file's directory is removed in a per-directory (.htaccess) context.

I'd suggest:

RewriteEngine on
#
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^((user|admin).*)$ https://%{HTTP_HOST}/$1 [R=301,L]

None of the other lines you posted are needed, unless you want the code to fail silently if mod_rewrite is not available.

Jim

seti

6:59 am on Mar 15, 2010 (gmt 0)

10+ Year Member



Thank you a lot Sir.

Even this code doesn't work on .htaccess:

RewriteRule ^user$ [%{HTTP_HOST}...] [R=301,L]

Don't know why. Aforementioned rule worked as it should on virtual host conf file.
And one more beginner question: what is $1 mean in your code?

jdMorgan

2:46 pm on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In order for your test rule above to work, it must be installed in the .htaccess file which will be accessed when the URL [user...] is requested, and because of your end-anchored pattern, it will work *only* for the exact URL-path "/user" -- if anything else follows "/user" then the rule won't be invoked.

You also need to delete your browser cache every time after making *any* change to server-side code, whether that be in a config file, .htaccess file, script, html page, css, JS file, etc. Forcing a page reload may not be sufficient due to different browser caching implementations, so I suggest that you completely delete the cache.

You're asking about the $1 back-reference in the rule I posted. This $1 refers to the matched contents of the first (outer) parenthesized sub-pattern in the RewriteRule pattern. Therefore, in my code, the requested URL-path starting with "/user" or "/admin" and including anything that follows it, is 'copied' from the http URL into the https URL, so that only the http/https protocol is changed by the redirect -- The originally-requested URL-path is retained.

However your question also implies that you have not read the Apache mod_rewrite documentation at apache.org, and I should warn you that using mod_rewrite without doing so can have serious detrimental effects on the healthy operation and search rankings of your site. This is server config code, and should not be trifled with.

Jim