In the following example I'm trying to redirect the directories login and blog with their respecting subdirectories back to the site root.
example.com
login
address
profile
blog
page1
page2
page3
contact
login
address
profile
Best attempt so far:
RedirectMatch 301 /(login|blog)(.*) /
The problem here is that pages like example.com/contact/login/address will redirect to example.com/contact/ which makes absolutely sense, but how can I avoid this? Or rather is ther something like this:
RedirectMatch 301 {SITE_ROOT}/(login|blog)(.*) /
You need to use start anchor ^ in your regex:
RedirectMatch 301 ^/(login|blog)(/.*)?$ /
Related
I need to redirect a url like this:
https://zzz.com/Class/Today-Class-100
to
https://zzz.com/otherfolder/
BUT leave a url like this
https://zzz.com/de/Class/Today-Class-100
alone. Right after the .com we have language directories, and those should not redirect. Only the first one should - how do I redirect an EXACT URL and leave ones with any difference alone?
This did not work:
RedirectMatch 301 ^/Class/Today-Class-101$ https://zzz.com/otherfolder/
as it still redirected with a subfolder in front of
/Class/Today-Class-101$
I am trying to redirect the following:
Redirect 301 /fruit/ https://www.website.com/vegetables/
^ The above works great IF the user clicks a link. However, should the user type-in the URL and omit the trailing slash after 'fruit' then it throws a 404.
So, my question is, how do I make all reference to /fruit/ or /fruit work correctly under 301 protocol?
Thanks for all replies
Google was my friend: the answer is >
RedirectMatch 301 /fruit/?$ https://www.website.com/vegetables/
my homepage url is like this http://mysite.com/sub/
I just want it to redirect to new url something like this http://mysite.com/sub/home?lang=en
here's my code
Redirect 301 /sub/ /sub/home?lang=en
Problem/error:
the new url becomes like this http://mysite.com/sub/home?lang=enhome
there's unnecessary home concatinated after en
how can I removed this? Or is there something wrong with my code?
don't know there's might be already same question like this
This is because the Redirect directive "connects" 2 path nodes, and you've got one inside the other (/sub/home is inside /sub). For example, if the directive looks like this:
Redirect 301 /a /b
This means when someone requests http://mysite.com/a/foo/bar they get redirected to http://mysite.com/b/foo/bar. What happens when you get redirected to /sub/home is that you get redirected again because /sub/home matches the pattern /sub, and the home gets appended, thus /sub/home?lang=enhome.
You can try using RedirectMatch instead, which doesn't "connect" path nodes:
RedirectMatch 301 ^/sub/?$ /sub/home?lang=en
Or mod_rewrite:
RewriteEngine On
RewriteRule ^/?sub/?$ /sub/home?lang=en [L,R=301,QSA]
I got some problem on redirecting something.
I tried to use Redirect 301 /link/link/link to /link/link
Is there a way to make it more easier coz there are 100+ links I need to redirect.
Like this
/blog/category/energy-savings/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
To the Homepage?
Thanks
The directive:
Redirect 301 /link/link/link /link/link
Doesn't do what you think it does. It's mapping to nodes together, meaning a request for:
/link/link/link/foo/bar.html
gets redirected to:
/link/link/foo/bar.html
So maybe you need to fix that. You could try:
RedirectMatch 301 ^/link/link/link/?$ /link/link
so the nodes aren't connected like in the Redirect directive. As for trying to "fix" this problem of yours, you can try:
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/\4/ /
This redirects anything that looks like: /blah1/blah2/blah3/same/same/same/ etc. to the homepage at /. The \4 matches the 4th path in the URI, so if anything after the 3rd path repeats, then it redirects.
i want to redirect a page:
http://www.mysite.com/samplepage/
Pages like http://www.mysite.com/samplepage/samplepage2/ shall not be redirected.
This is my current .htaccess code:
RedirectMatch 301 ^/samplepage/ /new-samplepageurl
The problem:
This one also redirects url's like:
http://www.mysite.com/samplepage/samplepage2/
Can I limit the redirect only to that one exact path?
Try using this:
RedirectMatch 301 ^/samplepage/?$ /new-samplepageurl
The $ sign marks the end of string, and ? mark makes the trailing slash opional (e.g. both /samplepage and /samplepage/ will be redirected)