Mod Rewrite For All URLs That Contain Certain String - .htaccess

How can I go about redirecting all instances of a URL that containt -2.html with just .html?
So for instance I would want www.url.com/slug-2.html to redirect to www.url.com/slug.html

You can use the following Redirect :
RedirectMatch ^/([^-]+)-2\.html$ http://url.com/$1.html

Related

.htaccess redirect / rewrite condition with exact match

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$

How do I set a proper .htaccess file for 301 in bludit?

I have 12 old url .html an d want to redirect to new url without .html like that /blogpost.html > /blogpost. What I'm writing in .htaccess
Use Redirect:
Redirect permanent /blogpost.html /blogpost
Instead of twelve Redirect statements, one for each of your old urls, you could stuff them into a single, though probably a little confusing RedirectMatch, too:
RedirectMatch permanent ^/(blogpost|oldpage|ancienturl|longago)\.html$ /$1
See http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect and http://httpd.apache.org/docs/current/mod/mod_alias.html#redirectmatch for details.

Domain redirecting to a different domain with a slightly modified path

So I want to redirect
domain2.com/path/url to a different domain, domain1.com/pathDurl where the / is replaced by a D or other character?
Is this possible?
You can use the following redirect in /domain2/.htaccess :
RedirectMatch 302 ^/path/(.+)$ http://domain1.com/pathD$1
This will redirect :
http://domain2.com/path/foobar
to
http://domain1.com/pathDfoobar

htaccess Redirect 301 : mysite.com/sub/ to mysite.com/sub/home

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]

Redirect only one page Page without affecting other urls with the nam in path

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)

Resources