Mobile app version of vmapp.org
Login or Join

Login to follow query

More posts by @Kristi941

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

You can do this with a single RewriteRule. The trick here is to only check for valid username characters, not everything (ie. .* - I wouldn't have thought your usernames could literally be anything?). This would also be more efficient since not every request will match and be processed.

For example, assuming your usernames can only consist of upper/lowercase letters and numbers then:

RewriteRule ^([a-zA-Z0-9])$ /remote.php/carddav/addressbooks//contacts [R=301]


This is very similar to your initial attempt. Note that the RewriteRule pattern in per-directory .htaccess files does not begin with a slash, however, if this rule was used in your server config then it would!

This also naturally avoids the rewrite loop since "/remote.php/carddav..." will not match as a valid username (specifically . and / would fail to match).

You could also limit the length of the username, to say between 4 and 20 characters...


^([a-zA-Z0-9]{4,20})$.


If you needed to match any character except a slash (a slash would surely break your destination URL?) then you could use a pattern like:


^([^/]+)$

10% popularity Vote Up Vote Down


 

@Kristi941

Problem fixed through one heck of a lot of perseverance. I really should take the time to learn all of mod_rewrite at some point in timeā€¦

In short I solved the redirect loop with the following condition:

RewriteCond %{REQUEST_URI} !^/remote.php/


The above condition basically matches everything that isn't remote.php/ANYTHING_HERE

I then used the above RewriteRule

RewriteRule ^(.*)$ /remote.php/carddav/addressbooks%{REQUEST_URI}/contacts [R=301]


The RewriteRule is almost identical to my previous comment with the difference being it uses the actual request (which is a username in my case and includes a slash - / - so does not need one at the end of addressbooks).

The end result is a full permanent redirect and a handy short URL for me to use when accessing carddav.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme