Redirect one URL to another on same domain with htaccess - .htaccess

I am currently building a new website. The old website has different url's than the new one. Now i want to redirect, using htaccess.
Before going live, i want to test my rules locally.
The old website uses url's of this format:
www.domain.de/?content=whatevercontent
The new website uses url's of this format:
www.domain.de/index.php?content=differentcontentname
Which are rewritten (in the htaccess file, using several RewriteRules) to this format:
www.domain.de/nicecontentname
I tried Redirects like this:
Redirect http://domainfolder/?content=whatevercontent http://10.3.10.69/domainfolder/nicecontentname
This does not work.
After going live it should work like this:
Redirect http://www.domain.de/?content=whatevercontent http://www.domain.de/index.php?content=differentcontentname
..and then be rewritten to the nice url.
My Redirect-Rules just won't apply, i tried it in all combinations i could think of, with or without http, with or without the containing folder, using the already rewritten url or the actual one, etc..
Any ideas on this issue?

You can not use Redirect directive to manipulte querystrings. Here is a mod-rewrite example that works for QueryStrings :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/\?content=this
RewriteRule ^$ /index.php?content=that [L,R]
This will temporary redirect /?content=this to /index.php?content=that
To make the redirect permanent (browser cacheable) change R to R=301.

Related

Htaccess redirects with parameters

I am trying to migrate a shop to another system, and would like to redirect my directories.
E.g. www.oldshop.eu/stuff to www.newshop.eu/stuff/
That I do by using
redirect 301 /stuff/ www.newshop.eu/stuff/
That works well, however my current shop has pages of the directories indexed, like:
www.oldshop.eu/stuff/?p=2
That I dont want to transfer to the newshop, however I can see on search console that this is being done. Seems my redirect takes everything after the /stuff/ and just putting it over?!
How can i avoid this so that all url with ?p= or other parameters are being avoided?
Br. Brian
You can use the rewriting module here:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old\.example\.com$
RewriteRule ^/?stuff/?$ https://new.example.com/stuff/ [QSD,R=301,L]
PS: It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues on the client side.

301 Redirect Rules for a Blog Migration

I want to implement 3 redirect rules for a blog migration where each page will be shifted to a sub-folder structure. It currently sits at a sub-domain.
I can't really screw this one up and want to make sure I nail the correct generic rules for the 3 type of existing URLs:
Homepage
Current:
https://blog.lang.example.com
Goal:
https://www.example.com/lang-country/news/
Category
Current:
https://blog.lang.example.com/category/category-name
Goal:
https://www.example.com/lang-country/news/category/category-name
Post
Current:
https://blog.lang.example.com/yyy/mm/dd/article-name
Goal:
https://www.example.com/lang-country/news/yyy/mm/dd/article-name
Is this something you can help?
Unless you have other URLs that you don't want to be redirected then you can do something like what you require with a single redirect near the top of the .htaccess file in the subdomain's document root.
For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.([^.]+)\.example\.com [NC]
RewriteRule (.*) https://www.example.com/%1-country/news/$1 [R,L]
Where %1 is a backreference to the lang subdomain in the requested host.
However, you still have the problem of where country should come from in the target URL, since this is not present in the source URL. You will either need to default this to something or implement some kind of lookup based on the language. This would need server config access (to configure a RewriteMap) if you wanted to do this in .htaccess. Or, you implement this redirect entirely in your server-side script (eg. PHP).
Note that this is currently a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect once you have tested that everything is working OK (if a 301 is required). 301 redirects are cached hard by the browser so can make testing problematic.

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

301 redirect adding unnecessary query string to new url

I'm trying to set up 301 redirects from a subdomain to the main domain, but I'm running into issues when the old urls have query strings.
The new site is running PHP as aFastCGI process. I tried using the solution listed here: https://expressionengine.stackexchange.com/questions/5188/301-redirects-appending-query-string
and it works for most urls, but not when the old urls have query strings.
For example,
RewriteRule ^old-blog/title/?$ http://www.example.com/new-blog/title/ [L,R=301]
works to redirect old.example.com/old-blog/title to www.example.com/new-blog/title but I'm
having trouble with pages that include query strings. I tried using this:
RewriteRule ^Default\.aspx?/?$ http://www.example.com/new-blog/ [L,R=301]
but a url like old.example.com/default.aspx?title redirects to www.example.com/new-blog/?title instead of just www.example.com/new-blog/
Can I stop the query strings from being appended to the new urls?
Also is there a way to just do a blanket redirect of every link that ISN'T already redirected using the first method above to www.example.com/new-blog/ ?
Not sure if it matters but the new site is running ExpressionEngine and using htaccess to remove index.php from the urls, but the htaccess rules above are on the old subdomain.

Htaccess Rewrite to remove directory

I need help creating a rewrite string to remove the ~12345678/ from the following: www.example.com/~12345678/file.
This doesn't appear on every link. What happened is during development links were created using the development url, there are a couple dozen links that appear this way. The rest of the links are fine. I already use a rewrite to remove index.php.
Try this (this will redirect any request for www.example.com/~12345678/dir/file.ext to www.example.com/dir/file.ext) :
RewriteRule ^\~[a-zA-Z0-9]+/(.*)?$ /$1 [L,QSA,R=301]

Resources