Redirecting Many Dynamic URLs (301) - apache

I have a website that is generating dynamic URLs through categories and it outputs the same information on two separate URLs (In this example it's "buildings" and "houses")
I would like to redirect all URLs that have /buildings/ in the URL to the same one with /houses/ instead.
For example:
/buildings/united-states/arizona/tucson/
to
/houses/united-states/arizona/tucson/
There are many URLs like this and I would like to use a code that does this for all.
I have tried
RewriteRule ^buildings/(\d[^/]+) /houses/$1/ [R=301,L], but it didn't seem to work (it still pointed to the /buildings/ URL.
Appreciate all your comments and guidance, thank you!

RewriteRule ^buildings/(\d[^/]+) /houses/$1/ [R=301,L]
For some reason have a \d (shorthand character class) that matches digits 0-9 only, so this won't match the example URL. Also, [^/]+ would seem unnecessary if it can be followed by anything anyway.
Try the following instead:
RewriteRule ^buildings/(.*) /houses/$1 [R=302,L]
This matches /buildings/<anything>. The $1 backreference holds the <anything> part.
Test first with 302 (temporary) redirect to avoid potential caching issues and only change to a 301 (permanent) redirect once you have confirmed it works as intended. You should clear you browser cache before testing.
This needs to go near the top of your .htaccess file, before any existing rewrites.

Related

How to create a redirect rule for 404 pages with many different path

Here my problem, the site I'm working on has many 404 pages, but they used to be the same pages with a different path
/mens/designers/mens/wales-bonner
/mens/designers/casual-shirts/wales-bonner
/mens/designers/coats-and-jackets/wales-bonner
etc.
THe client wants the redirect to go to the category, so
/mens/designers/mens/
/mens/designers/casual-shirts/
/mens/designers/coats-and-jackets/
I'm pretty sure, there must be a way to have regex rule to cover them all, but I can't seem to find how
Something like
RewriteRule ^/mens/designers/(.*)/wales-bonner /mens/designers/(.*)
but it doesn't work, I don't know how to group the middle part of the URL
Can anyone help ?
I see several potential problems with your rewrite rule:
You have a capturing group in the output URL rather than using $1 to use the result of the capturing group from the input URL.
^/ won't work in .htaccess, only in Apache .conf files. I' would use ^/? instead which will work in either context. That makes the starting slash optional.
You don't include an [R] flag on the rule to make it a redirect.
You don't include an [L] flag on the rule to prevent any following rules from interfering with it.
You can also add "mens/designers" to the capturing group so that you don't have to repeat it in the output. I would suggest:
RewriteRule ^/?(mens/designers/.*)/wales-bonner /$1 [R=301,L]

I want to set up redirects in htaccess from one domain to another but I've gone wrong somewhere

I have a website, let's say fruit.com, and currently I have a bunch of redirects set up that work just fine, so for example fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh.
I also used to have some redirects set up to allow me to use a short URL, so fru.it/mcintosh would redirect to fruit.com/apples.php?id=mcintosh.
So far so good. A few years ago, though, my short domain lapsed and I didn't renew. Recently I've purchased it again and I'm interested in getting the same setup back.
Now, though, the redirects from the short domain to the main domain aren't working, although I've used exactly the same code, so I'm at a bit of a loss for what's going wrong.
RewriteCond %{HTTP_HOST} ^www\.fru\.it$
RewriteRule ^([0-9]+)$ "http\:\/\/www\.fruit\.com\/apples.php?id=$1" [R=301,L]
although I've used exactly the same code
But the code you've posted won't redirect the stated example URL fru.it/mcintosh, since the code matches digits only, not letters.
Try the following instead:
RewriteCond %{HTTP_HOST} ^www\.fru\.it
RewriteRule ^(\w+)$ http://www.fruit.com/apples.php?id=$1 [R=301,L]
The \w shorthand character class matches upper and lowercase letters, numbers and underscore.
You don't need all the backslash-escapes in the substitution string.
Also bear in mind that the order of these directives can be important. This rule would likely need to go near the top of the .htaccess file to avoid conflicts.
Test first with a 302 (temporary) redirect to avoid potential caching issues. Clear your browser cache before testing.
Aside:
fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh
It would seem to make more sense that this would be a (internal) "rewrite", not a (external) "redirect"? The shortcode would then redirect to fruit.com/apples/mcintosh, not fruit.com/apples.php?id=mcintosh?

301 redirect for old urls with language parameter

I need a hand with some 301 redirects for my apache htaccess file. The old urls contain variables at the end and have structures like the following:
/furniture-248/category/570-shelves.html?lang=en
/all-products/furniture-248/shelves.html?page=2&lang=en
/store/product/asearch.html?path=7_632&lang=en&Itemid=284
The new urls don't contain parameters of this nature and would be simply of the form:
main-cat/subcat/sale.html
I tried a regular 301 redirect in the htaccess file which works for urls without parameters but those urls containing the ?lang=en simply don't work.
This is what I was trying:
Redirect 301 /furniture-248/category/570-shelves.html?lang=en http://www.domain.com/shelves.html
I'd be very grateful for any help and advice.
Many thanks in advance
You can't use the query string as part of a redirect like that. You have two options.
Option 1
Take the "?lang=en" part off and just redirect all instances of that URL, whatever the query string is.
Redirect 301 /furniture-248/category/570-shelves.html http://www.domain.com/shelves.html
This will leave the query string intact, so the new URL will include "?lang=en" if it is present, or any other query string.
But of course, you might need to only redirect it when it has the "?lang=en" part, or leaving the query string intact when redirecting might not be acceptable. In that case, it will need to be...
Option 2
Use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^furniture-248/category/570-shelves\.html$ http://www.domain.com/shelves.html? [R=301,L]
This does exactly what you asked for, redirecting /furniture-248/category/570-shelves.html?lang=en to http://www.domain.com/shelves.html and only that.
Note that..
The query string is matched separately.
The opening forward slash on the matching part is not used (because the fact you're in a website root level .htaccess file implies that opening slash).
The closing question mark on the redirect URL is important, as it tells the engine to drop the existing query string, which is what you want.
[R=301,L] means do a 301 redirect and don't process any more URL rewriting on this URL.
For the matching part in RewriteRule, the dot before "html" is escaped with "\" because dot has a special meaning in a regex.
Also for the matching parts, in both RewriteCond and RewriteRule, the ^ means the start of the string and the $ means the end of it, so that we are matching exactly that rather than it being possible for it to be part of a longer string.
And finally, if you're adding a number of these, you only need the "RewriteEngine On" part once, at the top. The other two parts are needed for each one.
Please be sure to test all redirects you add with this method as there is more to mod_rewrite than I have mentioned in this simple explanation.

multiple folder redirect

I have been trying variations of the following without success:
Redirect permanent /([0-9]+)/([0-9]+)/(.?).html http://example.com/($3)
It seems to have no effect. I have also tried rewrite with similar lack of results.
I want all links similar to: http://example.com/2002/10/some-long-title.html
to redirect the browser and spiders to: http://example.com/some-long-title
When I save this to my server, and visit a link with the nested folders, it just returns a 404 with the original URL unchanged in the address bar. What I want is the new location in the address bar (and the content of course).
I guess this is more or less what you are looking for:
RewriteEngine On
ReriteRule ^/([0-9]+)/([0-9]+)/(.?)\.html$ http://example.com/$3 [L,R=301]
This can be used inside the central apache configuration. If you have to use .htaccess files because you don't have access to the apache configuration then the syntax is slightly different.
Using mod_alias, you want the RedirectMatch, not the regular Redirect directive:
RedirectMatch permanent ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3
Your last grouping needs to be (.+) which means anything that's 1 character or more, what you had before, (.?) matches anything that is either 0 or 1 character. Also, the last backreference doesn't need the parentheses.
Using mod_rewrite, it looks similar:
RewriteEngine On
RewriteRule ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3 [L,R=301]

URL rewriting with mod_rewrite to provide RESTful URLs

The web server is Apache. I want to rewrite URL so a user won't know the actual directory. For example:
The original URL:
www.mydomainname.com/en/piecework/piecework.php?piecework_id=11
Expected URL:
piecework.mydomainname.com/en/11
I added the following statements in .htaccess:
RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)
I added the following statements in .htaccess:
RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3
Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)
What's wrong?
Try using RewriteLog in your vhost or server-conf in order to check the rewriting process. Right now you just seem to guess what mod_rewrite does.
By using RewriteLogLevel you can modify the extend of the logging. For starters I'd recommend level 5.
PS: Don't forget to reload/restart the server after modifying the config.
Here's a quick overview of what's happening:
RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
First, the question mark is supposed to be at the end.
$1 would (should) match anything that is not 'www' 0 or 1 times.
$2 matches anything that is not a character 1 or more times, which theoretically would match a blank space there but likely would never match anything.
Then it requires '.mydomainname.com' after those two groupings.
Your first two conditions are looking for two separate groupings.
I'm not sure exactly how you're trying to set up your structure, but here is how I would write it based on your original and expected URL's:
RewriteCond %{HTTP_HOST} !^www\.mydomainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomainname\.com$ [NC]
RewriteRule ^(\w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
Basically, your first condition is to make sure it's not the URL beginning with 'www' (it's easier to just make it a separate rule). The second condition makes it check any word that could possibly be in front of your domain name. Then your rewrite rule will redirect it appropriately.
Get rid of the last .htaccess line there in your question...
Someone please correct me if I typed something wrong. I don't remember if you have to have the '\' in front of '\w' and '\d' but I included them anyways.
You are doing it backwards. The idea is that you will give people the friendly address, and the re-write rule will point requests to this friendly, non-existent page to the real page without them seeing it. So right now you have it only handling what to do when they go to the ugly URL, but you are putting in the friendly URL. since no rule exists for when people put the friendly URL directly, apache looks for it and says "Object not Found"
So add a line:
RewriteRule piecework.mydomainname.com/en/(*.) ^/$3/en/piecework/$3?piecework_id=([0-9]+)(.*)
Sorry, that's quite right, but the basic idea is, if they put in the URL you like, Apache is ready to redirect to the real page without the browser seeing it.
Update
I'm way to sleepy to do regex correctly, so I had just tried my best to move your example around, sorry. I would try something more simple first just to get the basic concept down first. Try this:
RewriteRule www.mydomainname.com/en/piecework/piecework\.php\?piecework_id\=11 piecework.mydomainname.com/en/11
At the very least, it will be easier to see what isn't working if you get errors.