I'm trying to redirect this url:
https:///www.domain.com/courses/company/
to this one:
https:///www.domain.com/courses/
without also redirecting these:
https:///www.domain.com/courses/company/123/
https:///www.domain.com/courses/company/456/
I first tried this:
Redirect 301 /courses/company/ /courses/
but that also redirected the urls with the subdirectories.
Next I tried this:
Redirect 301 /courses/company/$ /courses/ ...because I thought the $ sign makes it match exactly that url.
But it didn't redirect anything.
Thanks!
The issue with the Redirect directive is that it does not allow to use "regular expressions" as you tried to do. It only implements a simple prefix string matching approach.
Instead you'd have to use the RedirectMatch directive which does support such expressions:
RedirectMatch 301 ^/courses/company/?$ /courses/
See the documentation of the RedirectMatch directive.
Instead you could also use the more flexible rewrite module:
RewriteEngine on
RewriteRule ^/?courses/company/?$ courses/ [L,R=301]
And a general hint: it usually is a good idea to start out with a 302 temporary redirection. And to only change that to the 301 permanent redirection once everything works as intended. That prevents nasty caching issues for your clients.
Related
Switching from ExpressionEngine to Wordpress and need to set up redirects in htaccess.
The paths to the articles will change from mysite.com/section/read/article-name
to mysite.com/article-name/. The section part of the path has six variants.
Unsure whether to look at redirectmatch or rewrite rule being a toatl novice with htaccess.
Thanks
Either would work, so I'll use RedirectMatch. To be specific about the section name, it would be:
RedirectMatch 301 "/(?:section|another-section|third-section)/read/(.+)$" /$1
Replacing your section names in there, separated by pipes.
Or for anything in a directory called read in a top level directory:
RedirectMatch 301 "/[^/]+/read/(.+)$" /$1
Since it is a one to one mapping and just the final part to be preserved, you don't need RedirectMatch. Redirect is sufficient
Redirect /section/read /
When it works as expected, you may set the status code to 301.
Redirect 301 /section/read /
If you want to use mod_rewrite instead, this would be
RewriteRule ^section/read/(.*)$ /$1 [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.
We want to redirect most , but not all, of a website wit a 301 redirect.
Will this work or is the first line to much ?
redirect 301 / http://designers-floor.nl/
redirect 301 /index.php http://designers-floor.nl/
redirect 301 /index.php/woonbeton http://designers-floor.nl/inspiratie/
The Redirect directive will redirect any url starting with the first fragment to the second fragment. The first directive will thus redirect the entire site. (docs)
I recommend testing with temporary redirects until you have accomplished what you want, because testing with permanent redirects will cause those redirects to be cached. Once everything works as expected you can make the redirects permanent. Make sure that the most specific url is always listed first, so that it is matched first.
If you cannot accomplish your goal with just the Redirect directives, you can either use RedirectMatch (docs) or the RewriteEngine of mod_rewrite (docs).
I'm trying to 301 redirect from '/en' or '/en/' to '/en/home' using .htaccess, but any attempt I do results into a redirection loop '/en/home/home/home/home/home/home...'.Shouldn't it be as simple as Redirect 301 /en /en/home?
Redirect based rule keep matching /en in redirected URL as well. You can use RedirectMatch for this with regex support:
RedirectMatch 301 ^/(en)/?$ /$1/home
Also make sure to clear your browser cache when you test this.
You have to use the full URL, example:
redirect 301 /folder_wrong/name.html http://website.com/folder-right/name.html
I'm using mod_rewrite. The .htaccess contains this line:
# redirect old references
Redirect 301 /folder/ http://www.donain.de/folder
This works fine execpt for one thing: All requests to subfolders are redirected as well. For example: /folder/hello is redirected to /folder. How can I prevent this?
I know that I can determine the start of an expression using ^ in a .htaccess file. But I can't figure out to say "stop this rule there".
You need to use RedirectMatch for regex support:
RedirectMatch 301 ^/folder/?$ http://www.donain.de/folder
I have a problem with RedirectMatch 301. It works fine on the top level domain and one variable attached, i.e.
http://xenolith.ws/ redirects to xeno-mods.com
http://xenolith.ws/explore redirects fine as well
http://xenolith.ws/mods/122 does not work
My RedirectMatch looks like this:
RedirectMatch 301 ^/(.*)$ http://xeno-mods.com/$1
What am I missing?
While testing your configuration, don't use 301, see this answer Tips for debugging .htaccess rewrite rules
Depending on your configuration and where you have this RedirectMatch, the leading / will already be removed or not. You might try
RedirectMatch .* http://xeno-mods.com/$0
or
RedirectMatch .* http://xeno-mods.com$0
You can also just use Redirect
Redirect / http://xeno-mods.com/
which redirects all requests to the new domain.
Don't forget to reload in your browser, because of your previous 301 tests, the browser might have already cached some results.
When the redirect works as you expect, you can insert the 301 status code again. But without it, the testing is much easier.