How to strip trailing URL characters in htaccess - .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.

Related

.htaccess Removing ampersand from URL?

I have been banging my head against a wall for a long time trying to figure out how to get rid of the last part of some of the URL's on my site. For example, I would like to rewrite this:-
http://www.mysite.com/335-protective-wrapping&page=prod
to this
http://www.mysite.com/335-protective-wrapping
There are about 2000 URL's with &page=prod at the end of them which I need to remove. Here's some more example URL's
http://www.mysite.com/335-protective-wrapping&page=prod
http://www.mysite.com/455-bubble-bags&page=prod
http://www.mysite.com/150-specialist-tapes&page=prod
I have tried many solutions but haven't come up with anything that works.
Any help would be much appreciated.
Thanks.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^([^&]+)&page=prod$ /$1 [L,R=301]
This should work:
RedirectMatch 301 ^/(.*)&page=prod$ http://www.mysite.com/$1
If you really want to take into account the query part (after the ?) of the source-URI you have to use RewriteCond plus RewriteRule, you cannot just use RewriteRule.
(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond)
Sorry, anubhava, that wont work for that reason.

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.

basic mod_rewrite question

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.

URL rewrite using mod_rewrite in .htaccess

I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]

Resources