Partial URL redirect with .htaccess - .htaccess

I'm need to redirect a a bunch of URL's through mod_rewrite. The URL structure is as follows:
www.mysite.com/somescript.php?&lang=asp&variable1&variable2
Needs to redirect to
www.mysite.com/somescript.php?&lang=php&variable1&variable2
So, basically, any URL with &lang=asp in it needs to be redirected to exactly the same URL but with &lang=php replacing &lang=asp.
Is there a way I can do this through .htaccess, perhaps with some sort of wildcard?
Thanks alot, I would appreciate your help.
Cheers,
Matt

Modifying the Query String
Change any single instance of val in the query string to other_val when accessing /path. Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond.
RewriteCond %{QUERY_STRING} ^(.*)lang=asp(.*)$
RewriteRule /path /path?%lang=php%2
Read this page for more info http://wiki.apache.org/httpd/RewriteQueryString

Related

Htaccess redirect long urls

I've got problem with redirecting old google maps urls to new one. I got new links (after component update), but i want make old ones work.
this is old one
domain.com/index.php?option=com_xmap&sitemap=1&view=xml
this is new one
domain.com/index.php?option=com_xmap&view=xml&tmpl=component&id=1
Please, help me redirect this :)
I think the following redirect would work. It redirects when there is no id= in the query string, and appends id=somenumber to the query string where somenumber is the number behind sitemap.
RewriteCond %{QUERY_STRING} !^(.*&|)id=
RewriteCond %{QUERY_STRING} ^(.*&|)sitemap=([0-9]+)(&.*|)$
RewriteRule ^ %{REQUEST_URI}?id=%2 [QSA,R,L]
A quick rundown of what I used here. %{QUERY_STRING} is a variable, just as %{REQUEST_URI}. A ! before a regex will negate that regex (ala 'doesn't contain'). %2 is replaced with the second capturing group of the last RewriteCond. In this case it will get replaced by whatever is captured by ([0-9]+). The flags are QSA (query string append), R (redirect, default is temporary redirect) and L (if this rule matches, stop rewriting; this doesn't affect the new request that is made). The documentation can be found here.

Can I use htaccess to Remove a phantom ? from the URL

Google is somehow indexing some phantom URL's for my website. I'm still trying to find the problem.
For example, everything in bold below is NOT supposed to be there.
http://www.mydomain.com/directory/sights?/kauai/sights/sights/kalalau_lookout
It should look like this:
http://www.mydomain.com/directory/sights
How can I strip out the ? mark in addition to everything after it and make it 301 redirect to the proper page?
Thanks,
John
From mod_rewrite
Modifying the Query String
By default, the query string is passed through unchanged.
...
When you want to erase an existing query string, end the substitution string with just a question mark.
...
You can remove all query strings with
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule .* http://www.mydomain.com$0?

Static (seo friendly) to dynamic url rewrite

Here is my problem...
I have a to redirect a link like this:
site.com/virtualfolder/some-seo-friendly-keywork
to
site.com/folder/page.php?id=some-seo-friendly-keyword
In site.com there is no real folder "virtualfolder", it is virtual.
I have to take "some-seo-friendly-keyword" and use it as a query string
I think i have to firts match the folder "virtualfolder" and then capture the "some-seo-friendly-keyword", but how?
The some-seo-friendly-keyword is a string of characters and digits plus hypens so something like this below is realistic?
RewriteRule ^virtualfolder/([a-zA-Z0-9-])? folder/page.php?id=$1 [L]
I'm still strudying and trying mod_rewrite and it is like voodoo to me! :-/
Thank you very much for your help or your suggestions
Try with this code in htaccess:
Using the original url to show:
Rewriterule ^virtualfolder/([a-zA-Z0-9_-]+)$ folder/page.php?id=$1
Redirecting to end url using RedirectMatch (use the full URL in the second part):
RedirectMatch 301 ^/virtualfolder/([a-zA-Z0-9_-]+)$ http://www.site.com/folder/page.php?id=$1
Redirecting to end url using mod_rewrite (use the full URL in the second part):
Rewriterule ^virtualfolder/([a-zA-Z0-9_-]+)$ http://www.site.com/folder/page.php?id=$1 [R=301,L,NE]
More info here

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 301 Redirect Appending Query's to End of URL

I am hoping someone can help with an unusual situation.
I have one main rewrite rule in place in my httpd.conf file which handles all of our dynamic content. The rule looks like this and works fine:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
The problem I have is that when I try to use .htaccess to create a simple 301 redirect, the query parameters are automatically appended to the end of the URL's so the final result looks like this:
http://www.example.com/category/page.html?&a=/category/subcategory/something/d/page/
Notice that the query string is appended to the URL when using .htaccess to create a 301 redirect.
I have solution for this on a case-by-case basis, but it's not practical to create a new rule each time I want to do a simple 301 redirect.
So, I am wondering if I can edit my "main rule" in any way so that when .htaccess is used to create redirects, the query parameters are not appended to the target URL.
Thanks in advance for any help you can provide.
If you have multiple simple redirects for which you want to suppress query string values you could put all the redirects in a RewriteMap (since you already have access to httpd.conf), and have one .htaccess rule that suppresses the query strings as below
place in htaccess
#if there is a match in the map
RewriteCond ${redirect_map:$1} !=""
RewriteRule ^(.*)$ ${redirect_map:$1}? [R,L]
place in httpd.conf
RewriteEngine On
RewriteMap redirect_map txt:/usr/local/apache/conf/redirect.map
contents of /usr/local/apache/conf/redirect.map
key followed by a space followed by target
directory/subdirectory1/subdirectory2/ example/category7/subdirectory/file.html
directory4/subdirectory2/subdirectory9/ example/category5/subdirectory4/file332.html
That's what your rule has defined it to do:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
It says to create a URL that will look like:
category/refine.cgi?&a=/foo/bar/
If you don't want that to happen, change your rule to be:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?

Resources