.htaccess rewrite rule - .htaccess

How do I write a rewrite rule for the following condition.
I have my directory setup as
/root/projectname/trunk/www/
I've setup site.local in hosts file to point to /root/
How do I make this request
http://site.local/projectname to grab data from /root/project_name/trunk/www/ instead of from /root/projectname/ ?
Right now I have it as RewriteRule ^projectname/*$ projectname/trunk/www/ [L]
That works for just http://site.local/projectname, but others like http://site.local/projectname/images/image1.jpg doesn't work.
Please enlight.
Thank you,
Tee

I think you should write you rule like following
RewriteRule ^projectname/(*)$ projectname/trunk/www/$1 [L]
but not tested

Ah I figured out what's cracking.
Since the mapping and mapped path both contains projectname, it just keeps on rewriting.
So I got to change the name to something else and it works.
Thanks,
Tee

Related

.htaccess, skip a variable level

I am struggling with a .htaccess issue.
I have a url like /files/download/1234/abc/def.pdf where /1234/ is a variable value and changes for each user.
However recently we transferred all the files to /userdownload/abc/def.png
How do i skip the variable level using my .htaccess file?
I tried stuff like:
^files/downloads/(.*)/(.*)$ http://www.example.com/userdownload/$2 [R=301,L]
However this did not give the result i needed as it also removed the /abc/ from the link.
Anyone knows how i can fix this issue?
You can use:
RewriteRule ^files/downloads/(\d+)/(.*)$ http://www.example.com/userdownload/$2 [R=301,L]
If your variable is only numeric.
Or you can use:
RewriteRule ^files/downloads/([^/]+)/(.*)$ http://www.example.com/userdownload/$2 [R=301,L]

htaccess rules to redirect in the same directory

I am new in htaccess rules. I have tried to search about my question but couldn't get the answer.
I have got a url like this mysite.it/test/libro/la-missione-cristiana/1959 and I would like to rewrite to mysite.it/test/index.php?page=libro&id=1959
I have the following htaccess file in my root folder and I have added the following line:
RewriteRule ^test/libro/([^/.]+)/([^/.]+) test/index.php?page=libro&id=$2 [QSA,L]
but It doesn't work.
I would really appreciate if someone could explain me the cause.
Thank you,
your match all but slash group '([^/.]+)' appears to group all non-slash and non-any-character, thus it will not match anything as the . matches any, and the plus require at least one match.
try remove the dots
RewriteRule ^test/libro/([^/]+)/([^/]+) test/index.php?page=libro&id=$2 [QSA,L]
should work for you.

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following:
So I have a ton of urls all with similar naming conventions - here is a sample of 2.
http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/
These urls need to redirect to:
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/
I could just redirect them 1 line at a time, but I'd like to use regex.
Here's what I was thinking so far but not working:
RewriteRule ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]
Basically i need to redirect any page right off the root that starts with "garrett-" to include the folder path of "garrett-metal-detectors".
Any thoughts would be MUCH appreciated. Many thanks in advance for your help.
if you want temprorary redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
if you want permanent redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
I'm am not an expert on Regular Expressions, but looks like your reg ex may be a bit off...
try:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
This is looking fro anything starting with "garrett" followed by any letter/number/hyphen combo.
Note: having "garett" in the destination part give you a loop of redirects, so you may have to choose a different word, or remove it all together...

agh - mod_rewrite remove '-xxx-XXX' from URL

bit of a nightmare. Clients sent out a big email with in links in them which have subsequently changed!
just need to change
forthcoming-events/event/skills-xxx-XXX
to
forthcoming-events/event/skills
so just removing the '-xxx-XXX'. Any help majorly appreciated as struggling to find a solution.
I'd have thought that something like:
RewriteRule \A/forthcoming-events/event/skills-xxx-XXX/*$ /forthcoming-events/event/skills/$1 [L]
...would do the trick. (That said, I've presumed that the "skills-..." bit is a directory.
RewriteRule ^(.*)-xxx-XXX$ $1 [R=302,NC]
the above works to match the rewrite the URLs!
cheers...

How to make a catch-all handler in an .htaccess file?

I want to create a rule at the end of an .htaccess file that catches everything that failed to match up until then.
How can I do that?
P.S. I've already tried everything :) Actually, I haven't, but it sure seems like it!
Update: Some people are replying with RewriteRule ^.*$ or an equivalent. This does not work! It will match everything including the other 'good' URLs.
There are actually some good answers here already, but you have responded with...
But then this matches everything including the good stuff.
This is because you aren't telling mod_rewrite to stop processing on a match. To do this, use the "L" tag after each rule, which tells mod_rewrite that "If this rule is matched, stop processing any further rules".
RewriteRule ^RSS/([^/\.]+)/?$ rss.php?Page=$1 [L]
You need to put this after EACH rule. Then, when you put the catch all at the end, it will only be hit if no other rule has been matched.
NOTE: if you are ALSO serving up resources that are not rewritten, like CSS, images, javascript files - you are honestly better off not catching all as you wouldn't want to rewrite their locations.
It sounds like you want to look at the RewriteRule directive. Quie possibly something similar to the following:
RewriteRule ^/(.+) {target}
This will match at the root URL (/) and redirect it to the {target} url.
RewriteRule .* page.php [L] ?

Resources