Redirecting using wildcard - .htaccess

First question in here, so be gentle with me:)
Much easier to explain with examples of the url.
This is one of the current url's
domainname.com/loc/window-repair/england/
We now need to lose the /loc/ part of the url so that it would now point to
www.domainname.com/window-repair/england/
The problem is that it isnt just one url, there are approxcimately 1000 urls pointing to various destinations in the UK all with the loc part in the url, and they all have sub directories.
eg
domainname.com/loc/window-repair/england/north-east/darlington
or
domainname.com/loc/window-repair/england/north-east/
or
domainname.com/loc/window-repair/scotland/
How can I put in a redirect so that the link still works but without the loc part.
Many Thanks

Try this in your htaccess file:
RedirectMatch 301 ^/([^/-]+)-([^/-]+)/(.*)/?$ /loc/$1-$2/$3

Related

.htaccess redirect to another page while retaining the complete query string

I am a little lost. I tried searching this site and the web at large, but I couldn't seem to find exactly what I need. So please forgive me if this has been answered before, I really tried to find it.
I have … inherited a .htaccess file with quite a lot of 301 redirects in the
Redirect 301 /shorthand /actual-link/actual-file.php
syntax (with a RewriteEngine On line somewhere high up in the file). I don't know exactly much about redirects, but that seems pretty straightforward to me. It just sits there and sometimes new shorthand links get added. There is some non-www to www, and http to https kind of stuff at the top, that's it.
Now the structure of the site changes, and two similar pages that process query parameters get consolidated into one. Basically there is
Old page: /path/subpath/index.php?some=query&parameter=or&other
New page: /other-path/file.php?added=parameter&some=query&parameter=or&other
I can't predict what parameters exactly will be part of the URL, I just have to take everything starting from the ? and append it to the new URL, that already has to include an added parameter, so everything after the ? follows ?added=parameter& .
I suppose that is not exactly hard, but alas, I lack the experience. And all I could find was something like "Take this specific defined query parameter you already know and set it as a path name" or vice versa, and I couldn't get that to work for my problem.
Is there a solution compatible with the syntax used elsewhere in the file? Or does that matter at all? Can I combine Redirect 301 … lines with RewriteCond … RewriteRule … commands? Does %{QUERY_STRING} help me somehow? If so, how can I figure out the correct syntax?
I would really appreciate if someone could point me in the right direction.
Many thanks in advance!

htaccess redirect with folder name

I need some help with htaccess redirects. Out site was wrongly crawled by google and as a result there are a lot of wrong urls being shown in webmaster tool. As an example:
articles/abcd/xyz
should be redirected as
articles/abcd/
articles/abcd/xyz.php
should be redirected as
articles/abcd/
articles/abcd/xyz.html
should be redirected as
articles/abcd/
So basically I am trying to mean always redirect to articles/abcd/ for varous wrong url types that i shown above. Please could you help
You can simply use RedirectMatch from mod_alias (documentation). We assume that the part after articles does not contain any / character. We redirect with a temporary redirect to the url without the suffix. Change "temp" to "permanent" after testing that this redirect actually works as you expect it to work.
RedirectMatch temp ^(/articles/[^/]+/).+$ $1

Redirect to target url and add query string

I'm trying to redirect just one page on a site to another one using htaccess. I'm new to this, but have been reading and reading everywhere to figure out how to do this, but without success.
I would like to redirect www.example.se to www.example.se/index.php?template=<templatename>
Is that possible and how would I approach that? So sorry if I'm asking a stupid question here, but really can't find a solution and it's driving me crazy.
Try:
Redirect / /index.php?template=<templatename>
Add a 301 right after Redirect if you want it to be permanent.

The trailing slash in home page

I will be really greatful if someone helps me with this.
Let's consider these 2 URLs (both returning 200 in the response header):
www.foo.com/something
www.foo.com/something/
Google considers these 2 URLs different despite both having the same content which leads to a duplicated content problem. To solve the issue it is advised to either use the 301 permanent redirect to redirect one URL to the other or use the rel="canonical" attribute. source
Wordpress blogs deal perfectly with this matter. When adding the trailing slash to my internal links, I was redirected to URLs without the trailing slash (301 response).
The problem is the redirect is only happening with internal pages. My homepage seem to return a 200 response with or without the trailing slash. Should I leave it as it is or force a redirect with the .htaccess file?
p.s.: The backlinks to my website have 2 different hrefs (with and without the trailing slash). Should I change those backlinks to a unique href or redirect one to the other?
Use this link to add trailing slash to end of your url
It doesn't matter whether your Backlinks are with slash or not, because after implementing techniques mentioned in above address, search engines will assume your pages only with slashes. Remember because of past indexing you should wait until former index to be deleted. or you may use 301 redirect to pages with slash. Basically this will take some time until search engines came again and find your redirect rules, too... .
By home page I assume you mean the page shown when you enter just the domain.
With or without the slash represents exactly the same URL. Nothing to worry about and nothing you can do.

htaccess redirects that ignore query string

I've recently inherited a webserver whose IP previously belonged to a well known band's forums.
Problem is I'm now drowning in 404 errors!
The URLs in question look like this:
http://[server_ip]/forum/ucp.php?mode=register&coppa=0
http://[server_ip]/forum/viewtopic.php?f=3&t=45330
http://[server_ip]/forum/index.php+++++++++++++++++++++++++++++++++++++++++++++++Result:+%ED%E5+%...
http://[server_ip]/forum/viewtopic.php?f=3&t=44597&start=0
In an ideal world I would like to redirect any traffic going to /forum/ucp.php, /forum/viewtopic.php or /forum/index.php elsewhere regardless of query string.
Or anything going to /forum/.* elsewhere, if that's doable.
I've tried a number of different solutions with little success, any help appreciated.
Assuming that you want to redirect all traffic to /forum/.* to http://mysite.com/somedirectory, which you can replace with the actual URL you wish to redirect to, you can add the following to the .htaccess in the root directory of your sites domain.
RewriteEngine On
RewriteBase /
#for any request starting with forum
RewriteRule ^forum/ http://mysite.com/somedirectory? [NC,L,R=302]
Be sure to keep the ? if you want to remove the query string params from the original query. If you want to make it a permanent redirect, change the 302 to a 301.
Don't really know what you have tried so far but this site will probably help you.
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#redirects

Resources