Redirect with numbers in URL - .htaccess

I have a redirect like this
Redirect 301 /dir/dir/843-987 /new_page
but instead to redirect to "/new_page" it does "/new_page/843-987"
Why? And how to avoid this?

That is how Redirect directive works as it appends current URI to target.
You should use more power mod_rewrite rules for finer control:
RewriteEngine On
RewriteRule ^dir/dir/843-987/?$ /new_page [L,NC,R=301]
Make sure to use a new browser to test the change or clear cache completely.

If 843-987 is a static term, you can use the following code:
RewriteRule ^dir/dir/843-987/?$ /new_page [L,NC,R=301]
If 843-987 is a range of number, you can use the following code:
RewriteRule ^dir/dir/(84[3-9]|8[5-9][0-9]|9[0-7][0-9]|98[0-7])/?$ /new_page [L,NC,R=301]
Breaking down for 843 to 987 range:
Parse Into Range:
843 - 849
850 - 899
900 - 979
980 - 987
Parse Into Block Regex:
84[3-9]
8[5-9][0-9]
9[0-7][0-9]
98[0-7]
Combining Into Regex Pattern:
(84[3-9]|8[5-9][0-9]|9[0-7][0-9]|98[0-7])

Related

How do I 410 a pattern of toxic Prestashop URLS?

I've been trying to 410 a pattern of a few thousands toxics URLS that got indexed by Google for one of my clients. I want every urls of the website that contain ?% to send a 410 response code so they got de-indexed.
Example of an URL :
https://www.example.com/fr/promotions?%25252525253Bid_lang=6&p=6
I've tried to put this in the .htaccess, above the If module section, as I found on another thread here, but it didn't work, any ideas ?
RewriteRule ^[a-z]shop($|/) - [G]
I want every urls of the website that contain ?% to send a 410 response code
Using mod_rewrite at the top of the .htaccess file:
# Send 410 for any URL where the query string starts with "%"
RewriteCond %{QUERY_STRING} ^%
RewriteRule ^ - [G]

Redirectmatch without append parameters

If you go to these URL:
http://www.borrat.com/iweb/es/comprar-barco-begur-venta-embarcaciones-costa-brava.htm
We need a redirection 301 to:
[...]/iweb/es/comprar-barco-begur-costa-brava.htm
If I use these expresion:
RedirectMatch /iweb/es/comprar-barco-emporda-venta-embarcaciones-costa-brava.htm /iweb/es/comprar-barco-emporda-costa-brava.htm?
The redirect URL finish with (?)
And if I do the same without (?) at the end, the sistem add some parameters from another expression.
Ex: /es/comprar-barco-emporda-costa-brava.htm?parameter=var
Is there any expression to limite the end for a redirection?
Thx and Best regards,
Manager Costa Brava Boats
You'll have to use mod_rewrite in order to have the query string entirely removed:
RewriteEngine On
RewriteRule ^iweb/es/comprar-barco-emporda-venta-embarcaciones-costa-brava\.htm$ /iweb/es/comprar-barco-emporda-costa-brava.htm? [L,R]

How to rewrite a url with multiple parameters to new url using .htaccess

I have rewritten my site from asp to php. I need to redirect a few pages with multiple parameters.
These are a few of the old url's:
mysite.co.uk/productlist_paged.asp?cid=1&offset=10
mysite.co.uk/productlist_paged.asp?cid=1&offset=20
mysite.co.uk/productlist_paged.asp?cid=1&offset=30
mysite.co.uk/productlist_paged.asp?cid=1&offset=40
to the following new pages:
mysite.co.uk/Compare/Roland-Digital-Pianos/43/1
mysite.co.uk/Compare/Roland-Digital-Pianos/43/2
mysite.co.uk/Compare/Roland-Digital-Pianos/43/3
mysite.co.uk/Compare/Roland-Digital-Pianos/43/4
I was hoping to keep the number 43 out of the redirect as this a number that will change when products are added/removed.
cid=1 equals Roland-Digital-Pianos and e.g offset=10 is number 1 at the end of the url
Any help welcome
You could go with something like :
RewriteEngine On
RewriteCond %{QUERY_STRING} cid=1&offset=([0-9]+)0
RewriteRule ^productlist_paged.asp$ /Compare/Roland-Digital-Pianos/43/%1? [R=301,L]
There is an extra 0 at the end of the RewriteCond, otherwise, %1 would be 10 or 20 instead of 1 or 2.
The extra ? at the end delete the QUERY_STRING in the redirect url.
Note : I didn't add the ^ and $ in the RewriteCond, so that your url doesn't necessarly start/end with this QUERY_STRING, ie productlist_paged.asp?test=1&cid=1&offset=10&test2=1 will also fit the RewriteCond and get redirected.
Hope it helps !

How to 301 redirect pages "up" a page

I'm a newbie and I'm trying to figure out the proper 301 redirect for the following pages. I hope I'm being clear here :) In my .htaccess file, I want to redirect pages "up" one pages without having to do every page individually.
My original pages looked like the following:
www.doctors.com/skin/california/best-skin-doctors-california/
www.doctors.com/skin/california/best-skin-doctors-california/?page=1
www.doctors.com/skin/california/best-skin-doctors-california/?page=2
....etc. ....up to like /?page=33
and more categories and states, like:
www.doctors.com/heart/new-york/best-heart-doctors-new-york/
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=1
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=2
...etc. .....again up to like /?page=24
I've since changed the page structure to eliminate the long URLs...like this:
www.doctors.com/skin/california/
www.doctors.com/skin/california/?page=1
www.doctors.com/skin/california/?page=2
etc.....and similarly....
www.doctors.com/heart/new-york/
www.doctors.com/heart/new-york/?page=1
www.doctors.com/heart/new-york/?page=2
etc.
How can I "bulk" redirect the original pages with the long URLs to the newer, shortened version in my .htaccess file? Thank you very much for your time and consideration!
Using mod_alias, you can simply add this in the .htaccess file in your document root:
RedirectMatch 301 ^/([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/
But if you need further restrictions on how the redirect works, you can use Apache's mod_rewrite module. Taking a look at the RewriteCond directive, you can impose conditions on a rule and put everything in .htaccess. The main rule will look very similar to mod_alias' RedirectMatch. Example:
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
In both cases, the query string (the page=3 part) is simply appended to the new target. Looking over the different things you can do with RewriteCond, say if you wanted to exclude this rule when requests are made for something like /images/ or /themes/:
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/themes/
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
So, if the request doesn't start with /images/ and the request doesn't start with /themes/, then apply the rule. This example would make it so a request for http://host.com/themes/subSilver/magic-icons/ don't get redirected to http://host.com/themes/subSilver/.

.htaccess/mod_rewrite: West <--- Main Site ---> East

I would really appreciate anyone's help with a mod_rewrite question. I don't know regex and am not familiar with .htaccess directives, so I can't think of how to solve the problem I am having.
Below is a description of what I am trying to accomplish...
3 websites:
http://www.example.com/ or http://example.com/
http://east.example.com/
http://west.example.com/
Have pages on main site that need to be redirected to other sites:
http://www.example.com/location1
http://www.example.com/location2
http://www.example.com/location3
http://www.example.com/location4
http://www.example.com/location5
http://www.example.com/location6
Need to redirect some pages to one website based on location, e.g http://east.example.com/:
http://www.example.com/location1 redirect to http://east.example.com/location1
http://www.example.com/location2 redirect to http://east.example.com/location2
http://www.example.com/location3 redirect to http://east.example.com/location3
(also without www) e.g.
http://example.com/location1 redirect to http://east.example.com/location1
Need to redirect some pages to another website based on location, e.g http://west.example.com/:
http://www.example.com/location4 redirect to http://west.example.com/location4
http://www.example.com/location5 redirect to http://west.example.com/location5
http://www.example.com/location6 redirect to http://west.example.com/location6
(also without www) e.g.
http://example.com/location4 redirect to http://west.example.com/location4
Want a rewrite rule that works like:
If domain name is www.example.com or domain name is example.com
And domain name is not east.example.com
And domain name is not west.example.com
And "somelocation" (in the east) is in the URL
Then Rewrite(Redirect) URL to http://east.example.com/.../somelocation/.../somepage
NOTE: the three websites actually share the same codebase, so the URLs all point to the same location/.htaccess file. So, I tried using a basic redirect, and ended up with an error that said... can't open page because of too many redirects.
If anyone knows the answer and can help with this, I would really appreciate the help!
EDIT: Example of expected results
Original URL:
"http://www.example.com/locations/eastend-web-page"
Rewritten URL:
"http://east.example.com/locations/eastend-web-page"
Please try to fill a rewritemap file (see here) to make a correspondance with the URLs that are part of your "east" domain. You may declare it like this:
RewriteMap mapmaintoeast \
dbm:/web/htdocs/yoursite/rewriterules/mapmaintoeast.map
Regarding your sample, your map file may be filled with things like:
eastlocation1 mainlocation1
eastlocation2 mainlocation2
eastlocation3 mainlocation3
...
I've called them 'east' and 'main' to distinguish clearly. These examples are supposed to be URLs.
Do the same for west:
RewriteMap mapmaintowest \
dbm:/web/htdocs/yoursite/rewriterules/mapmaintowest.map
In that file:
westlocation1 mainlocation4
westlocation2 mainlocation5
westlocation3 mainlocation6
...
Then here you go for the hard part:
# This cond put any non-empty string into "%1" (= parenthesis + first cond):
RewriteCond %{QUERY_STRING} (.+)
# The following rule doesn't touch the URL, but
# will try to search into the map file and
# create fill an environment variable called MAINTOEAST with the
# string found and if not found, assign MAINTOEAST to "notfound"
RewriteRule . - [QSA,E=MAINTOEAST:${mapmaintoeast:%1|notfound}]
# if MAINTOEAST not empty and different from "notfound":
RewriteCond %{ENV:MAINTOEAST} !^$
RewriteCond %{ENV:MAINTOEAST} !(notfound)
# ok found => redirect to east:
RewriteRule (.*) http://east.example.com$1 [QSA,R=301,L]
# Now do the same with west (no comments needed (see previous)):
RewriteCond %{QUERY_STRING} (.+)
RewriteRule . - [QSA,E=MAINTOWEST:${mapmaintowest:%1|notfound}]
RewriteCond %{ENV:MAINTOWEST} !^$
RewriteCond %{ENV:MAINTOWEST} !(notfound)
RewriteRule (.*) http://west.example.com$1 [QSA,R=301,L]
This should work.
I hope I've given you enough clues to finish the job ;)
If you don't have enough clues...
Two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
RewriteEngine On
# If domain name is www.example.com or domain name is example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
# And "somelocation" (in the east) is in the URL,
# Then Rewrite(Redirect) URL to http://east.example.com/.../somelocation/.../somepage
RewriteRule ^(.*somelocation.*)$ http://east.example.com/$1 [R,L]
You'd need to do the same thing for west. Note that if the domain name is www.example.com or example.com, it cannot be east.example.com or west.example.com

Resources