Forum Moderators: phranque

Message Too Old, No Replies

Apache RedirectMatch

Having some problems

         

jeking

8:54 pm on Feb 8, 2016 (gmt 0)

10+ Year Member



Hi,

I am trying to setup what I thought would be a simple RedirectMatch, however seems to be turning into a nightmare. My client has a site

http://www.example.ca/~username

And want anyone who accesses

http://www.example.ca/~UserName to be redirected to http://example.ca/~username. (Note the tilde in front of the URI)

I have attempted to the following to no avail

RewriteCond %{^~UserName} ^(.*)$ [NC]
RewriteRule http://www.example.ca/~username$1 [R=301,NC]
(not successful)

RedirectMatch 301 ^/~UserName/(.*)$ /~username/$1
(not successful)

RedirectMatch 301 ^/~UserName/ http://www.example.ca/~username [R=301,NC]
(not successful) Also note: I am using the NC due to the "~" which precedes the URI or directory. I have also tried without using the NC however produces the same results.

RedirectMatch 301,NC /^~UserName http://www.example.ca/~username

If anyone could shed some light on what I am missing here, it would be greatly appreciated.

Thanks in advance!

Regards,

jeking

[edited by: engine at 8:21 am (utc) on Feb 9, 2016]
[edit reason] examplified [/edit]

whitespace

11:43 pm on Feb 9, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



The "~username" bit looks like Apache's per-user (virtual) web directory. In which case you probably don't need to match this (and you probably can't match this using the RewriteRule pattern). (The other nagging thought is why are they using this to begin with - are they an academic institution?)

http://www.example.ca/~username/


This is presumably the document root for the site. I assume the site is only accessible via the /~username (virtual) subdirectory? And http://www.example.ca/ has nothing to do with your client's site?

Try the following in the .htaccess file in the document root.


RewriteEngine On
RewriteCond %{HTTP_HOST} =www.example.ca [NC]
RewriteRule (.*) http://example.ca/~username/$1 [R=302,L]


Change the R=302 (temporary) redirect to R=301 (permanent) when you are sure it's working OK. 301 redirects are cached by the browser so can make testing tricky.

TBH, some of your attempts have the basic syntax a bit mixed up. RewriteRule (mod_rewrite) and RedirectMatch (mod_alias) are two very different directives from different modules. RedirectMatch, for instance, has no concept of flags (such as NC), so any attempt to throw flags in the mix is likely to result in a parse error (500 internal server error).

Also note: I am using the NC due to the "~" which precedes the URI


The NC flag has nothing to do with the "~" (tilde). The NC flag (short for NOCASE) makes the match case-insensitive. So, for example, will allow "UserName" to match "username". The tilde (~) has no upper/lowercase twin, so the NC flag does not apply to this character. (You don't need the NE flag either, in case that was the one you intended to use?)

lucy24

3:42 am on Feb 10, 2016 (gmt 0)

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



RewriteCond %{^~UserName}

What is this intended to mean?

The NC flag (short for NOCASE) makes the match case-insensitive.

I was thinking it's exactly what you don't want to say if the rule is specifically about fixing casing (from ~UserName to ~username).

www vs. without-www doesn't matter, because the same redirect will take care of it all.

jeking

2:08 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



whitespace +

Hi,

Thanks for you reply, I tried your redirect as you outlined in your reply, however it produces the same results as I have been getting. The ~ before the username is related to our underlaying SSO software. I can't elaborate further on that. It does need to be caught by the web server however in order to navigate properly to the right destination.

Thanks again.

Regards,

jeking

jeking

2:49 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



lucy24

My intent in defining RewriteCond %{^~UserName} was to catch all responses beginning with ~ and UserName. However obviously failed miserably. The ~ in all cases is causing me much grief here. Essentially I am trying to do a location redirect from something that does not exist to something that does.

I should be able to do this with a simple RedirectMatch however that is proving to be not so simple.

And yes it is about specifically fixing casing from UserName to username.

Thanks for you reply!

Regards,

jeking

whitespace

6:32 pm on Feb 10, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Are these directives going in a .htaccess file? Where is this .htaccess file located?

Where does http://www.example.ca/~UserName resolve to? Does it get to the same place as /~username?

jeking

6:42 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



whitespace

Hi,

The .htaccess file is in the proper location. I have setup numerous redirects on this particular web server in the past. ~UserName does not exist. ~username does not exist. The directory username does however. The ~ was defined to all users document roots in the apache global configuration file. I have just inherited supporting this web server over the last year.

The URL is valid being http://www.example.ca/~username (of course not using the actual FQDN for the site) And can be accessed as such in a web browser. If you omit the ~ obviously you receive a 404 in your web browser.

Regards,

John King

jeking

6:43 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



whitespace

By "does not exist" I mean it is not a physical location on the web server.

jeking

7:08 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



whitespace

Yet another correction the ~ is not defined in the global configuration file, it relates to our LDAP configuration and how our usernames were created. There is a long history concerning this so I tend to only concentrate on when I need to know to make it work.

whitespace

7:26 pm on Feb 10, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



The .htaccess file is in the proper location.


So, on the filesystem path that maps to "http://www.example.ca/"?

I have setup numerous redirects on this particular web server in the past.


Do you have anything currently setup? any possible conflicts? Note that if you are already using RewriteRule (mod_rewrite) then you probably shouldn't be trying to use RedirectMatch (mod_alias). The mod_rewrite directives are likely to take priority, regardless of their order.

If you omit the ~ obviously you receive a 404 in your web browser.


I wouldn't have said that was entirely obvious since you say that the directory "username" does exist as a physical directory?

For "~username" (a virtual directory) to work, you must already have something configured to route this request?

Where does "http://www.example.ca/~UserName" (camelcase) currently end up? Is this a 404?

There is nothing particularly special about the "~" (unless you are using Apache's per-user web directories) - you should be able to match them (if you needed to). If you are unable to then it would perhaps suggest a conflict with your current config?

jeking

7:42 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



whitespace

So, on the filesystem path that maps to "http://www.example.ca/"?

Correct, yes.

Do you have anything currently setup? any possible conflicts? Note that if you are already using RewriteRule (mod_rewrite) then you probably shouldn't be trying to use RedirectMatch (mod_alias). The mod_rewrite directives are likely to take priority, regardless of their order.

No, nothing conflicting as there are no rules put into place for this particular client. The web server is hosting 320 web sites each configured with their own personal document root.

Where does "http://www.example.ca/~UserName" (camelcase) currently end up? Is this a 404?
Correct when typing that in a web browser a 404 is returned.
However when tying http://example.ca/~username you land on the web site (200 return)

There is nothing particularly special about the "~" (unless you are using Apache's per-user web directories) - you should be able to match them (if you needed to). If you are unable to then it would perhaps suggest a conflict with your current config?
I do believe we are using a per-user web directory in this case. Also, this is a Linux based web server so the ~ is interpreted as relevant

Thanks again for your followup.

Regards,

jeking

lucy24

8:33 pm on Feb 10, 2016 (gmt 0)

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



My intent in defining RewriteCond %{^~UserName} was to catch all responses beginning with ~ and UserName.

But that's not what %{blahblah} means. It sounds as if you meant to say
RewriteCond %{REQUEST_URI} ^/~UserName
except that you don't need to say this, because it's already in the body of the rule-- which is exactly where it belongs.

This thread has meandered a bit, but is the actual question purely about changing the case of the requested URI from CamelCase to lowercase? If so, there are two entirely different solutions, depending on whether you have access to the config file or not. The location of the rule itself doesn't matter, but you need to do at least one thing in config that can't be done in htaccess.

jeking

9:14 pm on Feb 10, 2016 (gmt 0)

10+ Year Member



lucy24

Hi again,

Yes, correct and I have already tried RewriteCond %{REQUEST_URI} ^/~UserName. Also I thought initially it was just about CamelCase to lowercase and if indeed that was the issue this redirect would already have been setup. The fact that our configuration requires the ~ in front of username is proving to be the show stopper here. Regex seems to be dealing with the ~ as it is returned in the browser however the actual redirect seems to not work. I have complete access to all configuration files. I was thinking about handling this either in the vhost file or the apache.conf file however I would rather stumble along with the .htaccess file due to the potential disruption it might have on the other users' sites.

I'm guessing you were thinking about modifying the RewriteMap?
<IfModule mod_rewrite.c>
RewriteMap lc int:tolower
</IfModule>

Thanks again for your reply.

Regards,

jeking

lucy24

11:02 pm on Feb 10, 2016 (gmt 0)

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



I'm guessing you were thinking about modifying the RewriteMap?

You wouldn't even need to modify it, since "tolower" is one of the built-ins; just make sure it's installed, and then give it a name, as you've done. By one of those weird Apache quirks, a RewriteMap can be declared only* in the config file-- but once it exists, you can use it in htaccess. (If you didn't have access to the config file, you would instead have to rewrite to a quick php script to do the same thing.)

Edit: We're talking about lots of different UserNames, right? Then the pattern would have to be something like
^~[A-Z][a-z]\w+/
so you're only redirecting requests that were wrong. If nothing except usernames will ever start in ~ then I don't think you need a Condition at all.

<IfModule mod_rewrite.c> 

This is your own server, right? Then there is absolutely no reason for an <IfModule> envelope; you already know you've got it. The envelope isn't even needed in htaccess files, unless there's a real risk that someone will come along and change the config file's Overrides settings behind your back. Unlike other envelopes such as <Files> or <Location>, the <IfModule> envelope has no effect on execution order, and it doesn't matter whether the content is related to the module. The envelope simply determines whether the lines are read at all. (I've experimented with this. The server doesn't bat an eye if you have an <IfModule> envelope running from the middle of one RewriteRule to the middle of another.)


* Yup, I carefully put the word "only" exactly here to avoid ambiguity.