• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1169
  • Last Modified:

Apache Rewrite rule seemingly having no effect, no log results either

I am trying to insert a rewrite rule which replaces:

www.domain.com/something/index.php?title=Special:userlogin

with

http://www.domain.com/login

I have placed the following into my httpd.conf file within the virtual host section but nothing is happening at all?

RewriteEngine On
RewriteLog logs/rewrite
RewriteLogLevel 9
RewriteRule ^/Special:Userlogin http://%{SERVER_NAME}/login/ [R,L]
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^title=Special:Userlogin
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteRule ^(.*)$ /login/ [R,L]

Nothing changes at the browser and when I look into the log I see nothing that relates to this rewrite rule?

Have I done something silly? Are there other directives I need to add or check that they exist in httpd.conf?

Oh, and I have restarted Apache.

Any input much appreciated.

Thanks,
0
pcoghlan
Asked:
pcoghlan
  • 6
  • 5
1 Solution
 
milanmkCommented:
Just try the following and see if it works.

RewriteEngine On
RewriteLog logs/rewrite
RewriteLogLevel 9
RewriteCond %{QUERY_STRING} ^title=Special:Userlogin
RewriteRule index.php /login/ [R,L]
0
 
pcoghlanAuthor Commented:
Thanks, nearly there. The rule is at least having an effect now which I suppose means one of the previous rules was not being met.

Anyway, here is the current status. I have the following in my httpd.conf file.

RewriteEngine On
RewriteLog logs/rewrite
RewriteLogLevel 9
RewriteCond %{QUERY_STRING} ^title=Special:Userlogin
RewriteRule index.php /user/login [R,L]

I would expect

www.domain.com/something/index.php?title=Special:userlogin

to end up as

www.domain.com/user/login/

but instead I end up at

www.domain.com/user/login/?title=Special:Userlogin

So, it is diverting me to the user/login BUT leaving the trailing text in place.

Any ideas?

Thanks,
0
 
pcoghlanAuthor Commented:
sorry, one typo. the last line should be:

RewriteRule index.php /user/login/ [R,L]

Note the additional trailing '/' after the login
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
milanmkCommented:
Query string is auto appended to the rewrite rule, if you dont want it then you will need an external redirect like the following.

RewriteEngine On
RewriteLog logs/rewrite
RewriteLogLevel 9
RewriteCond %{QUERY_STRING} ^title=Special:Userlogin
RewriteRule index.php www.domain.com/user/login/ [L]
0
 
milanmkCommented:
Sorry,

RewriteEngine On
RewriteLog logs/rewrite
RewriteLogLevel 9
RewriteCond %{QUERY_STRING} ^title=Special:Userlogin
RewriteRule index.php http://%{HTTP_HOST}/user/login/ [L]
0
 
pcoghlanAuthor Commented:
Thanks, tried this with same result.

I do get diverted to the new folder but the '?title=Special:Userlogin' is still there after the URL.

I tried changing the [R,L] to [L] but that results in the user seeing the URL they typed in.

Changing it back to [R,L] means I get to see the new URL.

I hope this all makes sense!
0
 
milanmkCommented:
Try this.

RewriteEngine On
RewriteLog logs/rewrite
RewriteLogLevel 9
RewriteCond %{QUERY_STRING} ^title=Special:Userlogin
RewriteRule index.php http://%{HTTP_HOST}/user/login/? [R,L]
0
 
pcoghlanAuthor Commented:
Sorry, no luck. I get a 404 with the ? after the login/
0
 
milanmkCommented:
Generally speaking, appending a "?" in the substitution tells mod_rewrite to remove any query string.

Try removing the slash after login if mod_dir is causing any problems.

RewriteEngine On
RewriteLog logs/rewrite
RewriteLogLevel 9
RewriteCond %{QUERY_STRING} ^title=Special:Userlogin
RewriteRule index.php http://%{HTTP_HOST}/user/login? [R,L]

Do you have any other rewrite rules above this rules?
0
 
pcoghlanAuthor Commented:
That did the trick!

Many thanks for your continued patience.

0
 
milanmkCommented:
Most welcomed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now