Htaccess redirects with parameters - .htaccess

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.

Related

301 Redirect with cet= parameter

I would like to make a 301 redirect of a url in http://website.com/example-old/?cet=3132 format to a page https://www.website.com/example-new/.
I have tried several times via .htaccess with the classic method:
redirect 301 /example-old/?cet=3132 https://www.website.com/example-new/
The redirect works, but I don't get what I want. In fact the final url becomes https://www.website.com/example-new/?cet=3132.
In short, the ?cet=3132 doesn't disappear and this is not good for me.
I found this invaluable resource which recommends a mod_rewrite method: 301 redirect for old urls with language parameter
Sure I'll try it, but I was wondering: will it work even with the cet=3132 parameter?
Thanks to those who can answer, best regards.
Sure you can use the rewriting module for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cet=3132$
RewriteRule ^/?example-old/?$ https://www.example.com/example-new/ [QSD,L,R=301]
The QSD flag will take care to remove the query string during the redirection.
It is a good idea to start out with a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. That prevents nasty caching issues.

Why does this htaccess work for a bit, and then doesnt?

I'm trying to redirect people to a new url of the login page,
It used to work, I made a few changes though, which kind of failed as I'm still trying to learn how to use htaccess,
Anyhow I brought the very first code back, and for some awkward reason, it won't work anymore,
Here's the code:
# external redirect using R=301 to /login from /index.php?act=Login
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php/?|)\?act=Login(&|\s) [NC]
RewriteRule ^/login? [R=301,L]
Is there any better way to achieve what I need?
Thanks!
Check the regular expression in the RewriteCond line. There is (index\.php/?|). The pipe character is either to much and should be removed or you missed the second option for the sub-pattern.
Alternatively you should check whether a simple Redirect instruction will be enough instead of using complex rewrite rule for a possibly simple task.
Basic syntax of redirect is:
Redirect [status] <old-url> <new-url>
for your purposes the following should work:
Redirect 301 /index.php?act=Login /login

trying to redirect a link in joomla or .htaccess

we moved our joomla site and rebuilt. in the process a link got moved that we need to be as it was before.
before:
www.mysite.org/kindergym
now it lives here:
www.mysite.org/education/kindergym
it would seem that it would be easy to go into com_redirect and do this. however, it only works for the following
mysite.org/kindergym without the www
with the www attached writing the old url returns a 404 error page, not a redirect.
i tried to make a separate redirect with the www too and it wouldnt let me. i tried a separate module with no success and have played around with the .htaccess file (although i am not very knowledgeable about htaccess).
could someone explain the reason why this would be an issue? the difference between the two. i tried calling my host and they were less than helpful and actually told me what i wanted to do couldnt be done LOL.
thanks.
I take it the solution you have would work if you redirect the entire mysite.org to www.mysite.org?
If so, create a .htaccess file in the website root. Put the following inside it:
########## Begin - Redirecting non-www request to www
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.org [NC]
RewriteRule (.*) http://www.mysite.org/$1 [L,R=301]
#
########## End - Redirecting non-www request to www
You also need to make sure mod_rewrite is enabled on the apache-server, but I think most providers support that.
I suggest you post your full .htaccess here. However I think all you need is this rule:
RewriteRule ^(?!education/).*)$ education/$1 [L,NC]
The other two answers are good! but better implement 301 redirect in httpd.conf since it's compiled once on server restart. The same code in .htccess is interpreted for each and every HTTP request!

htaccess selective redirect

I am trying to redirect all sub-directory pages to main directory, except of a few pages (e.g. (somepage1.html).
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(de|ru)/somepage1.html
RewriteRule ^([a-z]{2}|zh-CN|zh-TW)/(.*)$ /$2 [R=301,L]
Everything working except de/somepage1.html is redirected to home page (/), which is not acceptable. I wont it not redirected at all.
How can I achieve it
Thanks1
Well above rules are clearly excluding de/somepage1.html URL so it is most likely some other rule that is redirecting de/somepage1.html to /. Are you using wordpress or some other CMS tool by any chance? That might have its own rules in .htaccess file, check that please.
Also it would help to check web server's access log when this redirection happens.
the code I provided is working perfectly, so if somebody looking for this kind of a solution can use it without a fought.
The reason for not working is that my website were keeping cash and therefore was not renew frequently.

How do I use .htaccess to redirect to a URL containing HTTP_HOST?

Problem
I need to redirect some short convenience URLs to longer actual URLs. The site in question uses a set of subdomains to identify a set of development or live versions.
I would like the URL to which certain requests are redirected to include the HTTP_HOST such that I don't have to create a custom .htaccess file for each host.
Host-specific Example (snipped from .htaccess file)
Redirect /terms http://support.dev01.example.com/articles/terms/
This example works fine for the development version running at dev01.example.com. If I use the same line in the main .htaccess file for the development version running under dev02.example.com I'd end up being redirected to the wrong place.
Ideal rule (not sure of the correct syntax)
Redirect /terms http://support.{HTTP_HOST}/articles/terms/
This rule does not work and merely serves as an example of what I'd like to achieve. I could then use the exact same rule under many different hosts and get the correct result.
Answers?
Can this be done with mod_alias or does it require the more complex mod_rewrite?
How can this be achieved using mod_alias or mod_rewrite? I'd prefer a mod_alias solution if possible.
Clarifications
I'm not staying on the same server. I'd like:
http://example.com/terms/ -> http://support.example.com/articles/terms/
https://secure.example.com/terms/ -> http://support.example.com/articles/terms/
http://dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
https://secure.dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
I'd like to be able to use the same rule in the .htaccess file on both example.com and dev.example.com. In this situation I'd need to be able to refer to the HTTP_HOST as a variable rather than specifying it literally in the URL to which requests are redirected.
I'll investigate the HTTP_HOST parameter as suggested but was hoping for a working example.
It's strange that nobody has done the actual working answer (lol):
RewriteCond %{HTTP_HOST} support\.(([^\.]+))\.example\.com
RewriteRule ^/terms http://support.%1/article/terms [NC,QSA,R]
To help you doing the job faster, my favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
You use this tool when you want to check the URL and see if they're valid or not.
I think you'll want to capture the HTTP_HOST value and then use that in the rewrite rule:
RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/terms http://support.%1/article/terms [NC,R=302]
If I understand your question right, you want a 301 redirect (tell browser to go to other URL).
If my solution is not the correct one for you, try this tool: http://www.htaccessredirect.net/index.php and figure out what works for you.
//301 Redirect Entire Directory
RedirectMatch 301 /terms(.*) /articles/terms/$1
//Change default directory page
DirectoryIndex
According to this cheatsheet ( http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/ ) this should work
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1
Note that i don't have a way to test this so this should be taken as a pointer in the right direction as opposed to an explicit answer.
If you are staying on the same server then putting this in your .htaccess will work regardless of the server:
RedirectMatch 301 ^/terms$ /articles/terms/
Produces:
http://example.com/terms -> http://example.com/articles/terms
or:
http://test.example.com/terms -> http://test.example.com/articles/terms
Obviously you'll need to adjust the REGEX matching and the like to make sure it copes with what you are going to throw at it. Same goes for the 301, you might want a 302 if you don't want browsers to cache the redirect.
If you want:
http://example.com/terms -> http://server02.example.com/articles/terms
Then you'll need to use the HTTP_HOST parameter.
You don't need to include this information. Just provide a URI relative to the root.
Redirect temp /terms /articles/terms/
This is explained in the mod_alias documentation:
The new URL should be an absolute URL beginning with a scheme and hostname, but a URL-path beginning with a slash may also be used, in which case the scheme and hostname of the current server will be added.
It sounds like what you really need is just an alias?
Alias /terms /www/public/articles/terms/

Resources