basic mod_rewrite question - .htaccess

I am trying to write a rule which says
xyz.com/hotels_in_someplace will direct to xyz.com/test.php?place=someplace
by using the following in my htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^hotels_in_([a-z]+)$ test.php?place=$1 [L]
but its not working and I cant figure out why, I am running a wamp server as a dev. when i tried to run xyz.com/test.php?place=someplace (without the htaccess in the directory) it works but I suspect there's something my rule which is wrong.
additional:
whoops....my dumb ass mistake, mod_rewrite wasnt enabled...

Try it without the end-line anchor ($)
RewriteRule ^/hotels_in_([a-z]+) /test.php?place=$1 [L]
Also, did you allow the .htaccess files to be parsed?

Update: I didn't notice the RewriteBase in the question before so my point about the leading slash is null. However the RewriteRule does work on my machine.
Try:
RewriteRule ^hotels_in_([a-z]+)$ /test.php?place=$1 [L]
The path to test.php needs the leading slash. The way you are doing the rewrite you will be redirected to xyz.com/path/to/web/docs/on/machine/test.php and thats not what you want. :)
Also if you are you including a trailing slash in your test.
The above rule will not match xyz.com/hotels_in_someplace/.
To match the leading slash this should work:
RewriteRule ^hotels_in_([a-z]+)/?$ /test.php?place=$1 [L]

Your example works fine for me. Might want to make a few little adjustments (see my example, which captures uppercase letters, hyphens and underscores (for hotels in countries with more than one word (united-states, united_states)) and a trailing forward slash).
RewriteRule ^hotels_in_([A-Za-z_-]*)(/?)$ /test.php?place=$1
Hope that helps.

Related

my .htaccess rewriteRule is failing

I'm having trouble understanding why this rewrite isn't doing what its told.
NOTE: the first rewrite in my .htaccess file works properly so its not a problem with using mod_rewrite on local host.
i have URIs which i know will be in the format:
http://localhost/managerhub/my-manager.php?i=1&t=dashboard
when site goes live:
http://themanagerhub.com/my-manager.php?i=1&t=dashboard
my .htaccess file reads thus:
RewriteEngine On
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+) my-manager.php?i=$1&t=$2 [PT]
To achieve clean URls like:
http://localhost/managerhub/my-manager-1-dashboard
Ideally i dont really want the first capture group ([0-9]+) since i dont really want the 'i' value in the resultant
clean url - so ideally id like:
http://localhost/managerhub/my-manager-dashboard
However ive not even got the rewrite to work so far at all having tried:
leading forward-slash on the target (though i dont think it was necessary)
tried changing the '&' ampersand in the target to use &
removing the [PT] passthru flag replaced with and without [L] flag
tried most 'least' restrictive character classes in the pattern i.e. (.*) instead of ([0-9]+)
commented 'out' the first RewriteRule which works flawlessly BTW - so using the troublesome rule in isolation
Non of these have worked - the second rewrite rule has no effect on the target urls so i cant even see were the discrepancy is. I'm still new to mod_rewrite so sort of rely on an informative fail so i can work out were my reg-ex is wrong but i suspect its just being ignored since im getting 'zilch' back!!
Any help appreciated - maybe with a pointer to my folly.
thanks
Your htaccess file, I'm assuming, is in the "managerhub" directory. That's where those rules need to be. You may need to add a base as well:
RewriteEngine On
RewriteBase /managerhub/
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+)$ my-manager.php?i=$1&t=$2 [L,PT]
which you'd need to change when they get to the live site. You can remove the first capture group via:
RewriteRule ^my-manager-([a-z]+)$ my-manager.php?t=$1 [L,PT]
The L flag isn't exactly 100% needed.

mod_rewrite rule breaks when adding trailing slash 2 subdirectories deep

I'm very, very new to mod rewrite rules, and having trouble with one I'm using to redirect a site previously hosted in a subdirectory to a new domain. Here's the rule I'm using:
RewriteEngine on
RewriteBase /oldsite/ #also tried this without the trailing slash
RewriteRule ^.* http://newsite.com [R=301,NC,L]
This works perfectly until you get 3 levels deep and add a trailing slash to the redirected url. So, the results look like this:
olddomain.com/oldsite redirects to newsite.com [CORRECT]
olddomain.com/oldsite/ redirects to newsite.com [CORRECT]
olddomain.com/oldsite/subdirectory redirects to newsite.com/subdirectory [CORRECT]
olddomain.com/oldsite/subdirectory/ redirects to newsite.com [INCORRECT!]
I feel like I'm 99% there, but pulling my hair out a bit figuring out that last little bit. Any idea what I need to change?
Thanks!
Also, I tested placing the .htaccess file in the public html folder as well as in the /oldsite folder and there are no other htaccess files elsewhere on the site.
You're almost there, put this in the .htaccess in /oldsite on your server:
RewriteEngine On
RewriteRule (^.*) http://newsite.com/$1 [R=301,NC,L]
When you put the regex to match in parenthesis, it becomes available to use as $1. If you have more than one, $1 is the first, $2 the second, etc.
I suggest reading the manual page for mod_rewrite, specifically the rewriterule section (http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule).

Rewrite doesn't work without trailing slash

I have problems getting an URL to work WITHOUT entering trailing slash.
It's:
www.domain.com/shop/buy/products/show/range/
.htaccess Rewrite Rule is:
RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)/?$ _shop/products.php?trg=${productmap:$1}&range=$2 [L]
It works with trailing slash (which I don't want in the URL) but not without. I should also add that if I was to remove '/show/' from the URL (which I can't do though), it works without trailing slash, or if 'range' contains a dash '-', as in 'new-product', it also works.
However, this URL works with or without trailing slash:
www.domain.com/shop/buy/products/show/range/color
.htaccess Rewrite Rule for this URL is:
RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)/([A-Za-z0-9\-\,]+)/?$ _shop/products.php?trg=${productmap:$1}&range=$2&color=$3 [L]
How can I get the first URL to work without trailing slash? This might be something really obvious as I'm a recent newbie to using .htaccess but I have now spent hours staring at the code and reading forum posts about rewrites but not been able to resolve this. Thank you!
T'm not so sure, but have You try to put /? in braces: (/)?, I think this will work:
RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)([/]?)$ _shop/products.php?trg=${productmap:$1}&range=$2 [L]
You Can Test This Code
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^shop/buy/(.*?)/show/(.*)/(.*)$ _shop/products.php?trg=$1&range=$2&color=$3 [S,L,QSA]
RewriteRule ^shop/buy/(.*?)/show/(.*)$ _shop/products.php?trg=$1&range=$2 [S,L,QSA]
sample:
www.domain.com/shop/buy/products/show/range/
www.domain.com/shop/buy/products/show/range
redirect to:
www.domain.com/_shop/products.php?trg=products&range=range
and:
www.domain.com/shop/buy/products/show/range/color
www.domain.com/shop/buy/products/show/range/color/
redirect to:
www.domain.com/_shop/products.php?trg=products&range=range&color=color
one Another way is you Can Use Only This code
RewriteRule ^shop/buy/(.*?)/show/(.*)$ _shop/products.php?trg=$1&range=$2 [L,QSA]
after That split range in php By
list($range,$coler)=explode("/",$_GET['range']);
It is work too.

mod_rewrite rules not working as expected

I am using the following code
RewriteEngine on
RewriteRule ^/(certain|folders|on|server) - [NC,L]
RewriteRule (.*) http://newserver/blog$1 [L,NS]
I would expect it to ignore my list of folders, and redirect to everything else. Instead, it seems to always be redirecting to "newserver". Changing the "-" to a URL in the first statement does work, but I don't actually want those folders directed anywhere.
Why does my statement not work as expected?
Also, I have noticed that for all the folders not listed, I have "newserver/", while the ones I listed are just "newserver" (no uri)
You need to remove the leading slash in your rule. Rewrite rules in an htaccess file are given a URI to match against with the leading slash stripped off. Change your first rule by adding a ? after the slash:
RewriteRule ^/?(certain|folders|on|server) - [NC,L]
Or you can change it into a condition:
RewriteCond %{REQUEST_URI} !^/(certain|folders|on|server)
that will be applied to the rule that follows it.
Turns out my rules do work as expected. I was previously trying using these rules in apache.conf. Moving it to the sites "VirtualDirectory" tag seemed to do the trick

How to strip trailing URL characters in htaccess

I'm hoping someone can help with this one. I run a forum written in Perl, and the forum does something to URLs that is causing search engines to create duplicates.
I'm thinking that the best way of handling this is to sort it at the htaccess level.
As an example, the following 4 URLs all go to the same page, but search engines are seeing one entry with three duplicates:
http://www.domain.com/forum/YaBB.pl?num=1234567890
http://www.domain.com/forum/YaBB.pl?num=1234567890/2
http://www.domain.com/forum/YaBB.pl?num=1234567890/19
http://www.domain.com/forum/YaBB.pl?num=1234567890/22
I'm looking get htaccess to redirect anything that has a forward-slash somewhere in the last three characters, to a URL that has the slash and trailing numbers removed. Using the above example:
Redirect 301 /forum/YaBB.pl?num=1234567890/2 to /forum/YaBB.pl?num=1234567890
Alternatively, to re-write URLs from that subdomain to strip "/n" and "/nn"
Anyone have any ideas?
Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^cgi-bin/forum/YaBB\.pl\?num=([0-9]+)/[0-9]+$ cgi-bin/forum/YaBB.pl?num=$1 [R=302,L]
This should work, but let me know if it doesn't :) Also if it works, change the 'R=302' to 'R=301'
Try this rule:
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^num=(\d+)/
RewriteRule ^(cgi-bin/forum/YaBB\.pl)$ /$1?num=%1 [R=301,L]
Tested on local Apache installation -- works fine for me.

Resources