remove middle part of query from url with htaccess - .htaccess

searched on stackoverflow a bit, but nothing helpfull.
I want to do the following via htaccess.
I have this url:
site.com/location/usa/ca/los-angeles
and I want this:
site.com/location/ca/los-angeles
Basicly I want to remove the abbreviation of the country. Keep in mind, I have more then one country.
Any ideas?

To change all link like /location/1/2/3 to /location/2/3.
Use in your : /location/.htaccess:
RewriteEngine on
RewriteRule ^[^/]+/([^/]+/[^/]+)/?$ $1 [R=301,L]

Related

Link Redirect with htaccess

I want to redirect all type of links like that :
www.sitename.com/link1-XXX.html
to
www.sitename.com/link1-XXX/
I don't want to redirect all .html links. I want to redirect all html links which end with XXX.
Solution :
RewriteRule ^(.*)XXX\.html$ /$XXX/ [R=301,L]
You did not specified the question, just told us what you want to do, not what is your problem...
if your problem is, that you dont know how to redirect only links like you described, then put this in your .htaccess with RewriteEngine on:
RewriteRule ^/(.*)-XXX\.html?$ /$1-XXX/ [L,QSA]
I did not really tested this regex..
It should take anything that end with /"something"-XXX.htm and /"something"-XXX.html and redirect it to subaddr named as "something"-XXX/
if you would need append some GET params to the processed url, you should change the regex so it ends like this:
...html?(.*)$
meaning, there can be anything after htm or html

have a unique number in the url - do not match

This is how I would like to get my url to look at it any other way than before.
The problem is such that I must have forum and after forum content unique number that is in the database and after the url of the content.
I could imagine that it looked like this:
/forum/1/hello-world-stackoverflow-danmark/
now on my .htaccess file:
RewriteRule ^forum/([^/.]*)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]
and now it:
www.hello-world.com/forum/velkommen-til-traenigsmakker---sjaelland/
I will have its to
www.hello-world.com/forum/1/velkommen-til-traenigsmakker---sjaelland/
Be sure to have RewriteEngine On
The code you have should work, you can optimize it like this to ensure the numbers.
RewriteRule ^forum/([0-9]+)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]

htaccess 301 redirect all website pages with query to /

In the past website was full of pages with different basis. After redisign and with new concept the website consists of only 1 page. So I need to redirect all old pages to /.
Pages for example:
http://domain.com/catalog/index.html?c_id=145
http://domain.com/catalog/?c_id=116
http://domain.com/news/read.html?id=174
and so on.
In htaccess I've made several rules like this:
RewriteRule ^catalog(.*)$ http://domain.com/ [L,R=301]
But this rule don't give a full control, and the result is not satisfactory:
http://domain.com/?c_id=145
How can I get rid of this? I need to redirect all such links to http://domain.com without any additions. I think the {QUERY_STRING} can help, but I don't know how to do this correct.
Any help would be appreciated. Thanks!
You can add a ? at the end to create a blank query string:
# append "?" here ----v
RewriteRule ^catalog(.*)$ http://domain.com/? [L,R=301]

CFM redirect 301

On Google I have a site that has a bunch of old links to its pages, they are links like this.
/mainpage.cfm?linkId=84&LinkType=mainlink
I want to 301 redirect them with htaccess, but nothing I am trying works.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/architectural
RewriteRule .* /mainpage.cfm?linkId=84&LinkType=mainlink
Any Ideas, I have tried many varients of this, it seems the problem is the .cfm file.
Your question is a bit fuzzy. You say you want to rewrite from /mainpage.cfm?linkId=84&LinkType=mainlink, but then you also have that as the target of your RewriteRule. So I think some wires are crossed somewhere. Can you please update your question to include "I want to rewrite [this current URL example] to [the URL you wish the first one to end up at]". Also any other considerations that might require a RewriteCond, and any variations in the patterns.
Then we can get your rules/conditions sorted out.
To answer your exact question as asked, your RewriteCond will reject /mainpage.cfm?linkId=84&LinkType=mainlink because that does not match ^/architectural.
However I suspect this is not the question you mean to ask...
in mod_rewrite RewriteRule can only see the directory and file part of the URI and not the query string. So to match the query string you need to use RewriteCond.
e.g.
RewriteCond %{QUERY_STRING} linkId=(\d+)&LinkType=mainlink [NC]
RewiteRule ^mainpage\.cfm newpage.php?linkid=%1 [NC,L]
I have matched the linkId in the RewriteCond which I then refer to by %1 in the RewriteRule as this is the syntax for matching groups in a RewriteCond.
As #AdamCameron points out you don't state where you want to redirect to, but this should give you the tools to resove it.
You could perform the redirect within the ColdFusion page instead. Just add the following line to the top of the mainpage.cfm file (assuming you want every request of that page redirected). You could add some condition logic if you only want to redirect specific linkId and/or LinkType based on the URL parameter.
Again, if you want every request to the mainpage.cfm to redirect just add this to the top of that file (NOTE you need to change the url for the redirected page):
<cflocation url="http://host/architetural" statusCode="301" addtoken="no">
The statusCode attribute was added in ColdFusion 8 - so you must be running that or newer

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following:
So I have a ton of urls all with similar naming conventions - here is a sample of 2.
http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/
These urls need to redirect to:
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/
I could just redirect them 1 line at a time, but I'd like to use regex.
Here's what I was thinking so far but not working:
RewriteRule ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]
Basically i need to redirect any page right off the root that starts with "garrett-" to include the folder path of "garrett-metal-detectors".
Any thoughts would be MUCH appreciated. Many thanks in advance for your help.
if you want temprorary redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
if you want permanent redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
I'm am not an expert on Regular Expressions, but looks like your reg ex may be a bit off...
try:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
This is looking fro anything starting with "garrett" followed by any letter/number/hyphen combo.
Note: having "garett" in the destination part give you a loop of redirects, so you may have to choose a different word, or remove it all together...

Resources