301 redirect using rewrite - mod-rewrite

I recently rebuilt an ecommerce site and I need to put some redirects in it to account for the old category adn search results from the old siteto the new format. I have it almost working but it is carrying the query string to the new page. Here is what I have
RewriteCond %{QUERY_STRING} ^group_id=4$
RewriteRule ^searchResult.php$ http://website.com/category [L,R=301]
I have other rewrite rules in there that are working fine so I put this on the top so it would not interfere with anything. This does work but it carrys the query string to the page. Any ideas what I am doing wrong. I did go through a ton of posts on here, thats where I came up with what I have.
Thanks

An empty query string in the target will clear it.
RewriteRule ... ...? [...]

Related

Apache .htaccess mod rewrite

I need to rewrite this url schema
http://www.example.com/a/b/c/d.php?param=hi
to
http://www.example.com/a/b/d.php?type=c&param=hi
It's possible with mod_rewrite in .htaccess?
Thanks a lot for yours help!
[MORE DETAILS]
The base url of site is http://www.example.com/a/b/, and i have two type of customers, privates & sellers. I ask if is possible for customers call this url http://www.example.com/a/b/private/index.php, and internally rewrite it, with htaccess, to http://www.example.com/a/b/index.php?cust=private preserving the other GET parameters.
Well if the URL is always the same you can literally just write it one for one.
RewriteEngine On
RewriteRule ^a/b/private/index\.php$ /a/b/index.php?cust=private [QSA,L]
Ok, based on the Panama Jack's answer this is the standard rule for rewriting all page,
RewriteEngine On
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)$ $2.php?cust=$1 [QSA]

Mod_rewrite - don't display certain query string in the url

I know this question may have been asked several times, but to be honest I haven't yet found a complete answer for this.
I have this url:
modelDetails.php?manufacturerName=$1&manfuacturerID=$2&modelName=$3&modelID=$4&yachtCode=$5&lang=$6
Is it possible not to display yachtCode and lang in the url and still pass the values from page to page?
This is my htacces file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteCond %{QUERY_STRING} ^lang=(EN|DE|NL)$ [NC]
RewriteRule ^(.*)/([0-9]+)/(.*)/([0-9]+)/([0-9]+)$ modelDetails.php?manufacturerName=$1&manufacturerID=$2&modelName=$3&modelID=$4&yachtCode=$5 [L,QSA]
If you remove the data from the query string, then the only other way to access the data is to retrieve it from a cookie. But to set the cookie you'll have to have the yachtCode value appear in the query string at some point.
The only alternative would be to use POST (the mode used by a web form) instead of GET (the mode which is generally used). POST mode submits variables as part of the request, rather than adding the variables to the query string. But you can't force a hyperlink to use POST mode, so this is probably not of use to you.
In short, you probably can't hide yachtCode from the end user completely.

Apache Mod Rewrite Rule Special Condition

Im trying to redirect an old domain to its noew domain but there are some rules wheich I need to put in place and so far I havn't managed to get it quite right.
the old domain e.g www.old-domain.com has hundreds of folders names after UK towns like this:
www.old-domain.com/sheffield/
www.old-domain.com/london/
www.old-domain.com/essex/
inside each of these folders contains an index.html file and possible other directoreis and files.
I needs to redirect them to the new domain in such a way so that old domain/town maps to new domain/town but old domain/town/index.html doesnt put index.html on the new domain end however if the path after the town is anything other than index.html to redirect to it on the new domain.
Sorry that isn't the easiest to explain and not the easiest to read and undeerstand Im sure.
www.old-domain.com/sheffield => www.new-domain.com/sheffield
www.old-domain.com/sheffield/ => www.new-domain.com/sheffield/
www.old-domain.com/sheffield/index.html => www.new-domain.com/sheffield/
www.old-domain.com/sheffield/main.html => www.new-domain.com/sheffield/main.html
www.old-domain.com/sheffield/innerFolder/ => www.new-domain.com/sheffield/innerFolder
www.old-domain.com/sheffield/innerFolder/file.php => www.new-domain.com/sheffield/innerFolder/file.php
The two in bold above I managed to get working by this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^sheffield/(.*)$ http://www.new-domain.com/sheffield/$1 [R=301,L]
However Im really struggling to get old-domain.com/sheffield/index.html to not put .index.html on the new domain.
Can anyone shed any light on this before I pull my hair out staring at mod rewrite tutorial for any more hours?
Hint: The rewrite rules are processed on first matched basis.
You can put your exception before the main rule
After roughly 4-5 hours of trying different combniations and reading god know how many rewriterule tutorials I managed to get there. Heres the htacess file for just 3 locations which all work wonderfully now.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^sheffield\/index\.html http://www.new-domain.co.uk/sheffield/ [R=301,L]
RewriteRule ^sheffield/(.*)$ http://www.new-domain.co.uk/sheffield/$1 [R=301,L]
RewriteRule ^bolton\/index\.html http://www.new-domain.co.uk/bolton/ [R=301,L]
RewriteRule ^bolton/(.*)$ http://www.new-domain.co.uk/bolton/$1 [R=301,L]
RewriteRule ^coventry\/index\.html http://www.new-domain.co.uk/coventry/ [R=301,L]
RewriteRule ^coventry/(.*)$ http://www.new-domain.co.uk/coventry/$1 [R=301,L]

Rewrite Condition not working for Rewrite rule on individual pages

I am trying to write a rule that will capture any url that does NOT have sales/anything up to a .php or .php3 file and anything after that - if there is anything - and rewrite that to a new website as per below:
RewriteCond %{REQUEST_URI} !^(/sales/.*php3?).*
RewriteRule ^/sales/([^./]*)$ http://www2.domain.com/sales$1/index.shtml [R,L]
It captures if I put in www.domain.com/sales but if I put in just http://www.domain.com/sales/trucks.shtml if does not capture the individual pages.
Can anyone see what I need to do to get this to work correctly please ?
To clarify:
.If I put in url www.domain.com/sales, the site redirects to www2.domain.com/sales/index.shtml ....however if I put in the url www.domain.com/sales/trucks.shtml the condition is not picked up and the url does not rewrite to the ww2 site so I am stuck on the old page still ....thanks for your help
Alright use these 2 rules for your requirements:
RewriteRule ^sales/?$ http://www2.domain.com/sales/index.shtml [R,L,NC]
RewriteRule ^sales/(?!.*\.php3?$).*$ http://www2.domain.com%{REQUEST_URI} [R,L,NC]

Getting the original REQUEST_URI when using mod_rewrite AND mod_proxy

I'm using a custom.conf file for rewrites and codeigniter for some features of the site, mainly the articles.
My original url gets rewritten, so I have http://example.com/article-a101, this uses the custom.conf file to rewrite to codeigniter/article/read/101. I think I must send this as a proxy call using the [P] flag in mod_rewrite to make it rewrite again in codeigniters .htaccess file. Once it hits the code igniter .htaccess, it uses that mod rewrite structure to call the index file and use the article controller and the read function sending in the 101 as the parameter.
What I'm trying to figure it is how do I get the original url in the address bar as its not in the $_SERVER variable. Since I use the [P] on the first rewrite, request_uri has codeigniter/article/read/101.
custom.conf
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2 [P,L]
codeigniters .htaccess, fairly basic
RewriteRule ^(.*)$ index.php?/$1 [L]
Here's my current solution that I know there must be a better method for
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2?orig_url=%{REQUEST_URI}&%{QUERY_STRING} [P,L]
This stays hidden from the user, and I can access the original url through the query string, but doesn't seem like an elegant solution.
I'm pretty sure you cant do it any other way with mod_rewrite
but you could do it with codeigniter routing.
$route['^([_a-zA-Z0-9-]+)-a([0-9]+)$'] = "article/read/$2";
assuming your controller is named article and your function is named read
if you visited /article-a101
then $this->uri->uri_string(); would return article-a101 (the original url, which should be in your url bar now)
and $this->uri->ruri_string(); would return article/read/101 (where you actually are)

Resources