301 redirect with cut last word - .htaccess

I want redirect or delete last phrase from url with .htaccess:
https://example.com/en/word1/word2/random-word-with-numbers/lang-en-GB
to
https://example.com/en/word1/word2/random-word-with-numbers
What I shall do? I tried but doesn't work:
RewriteEngine On
RewriteRule ^word2/([a-z]+)\/([0-9]+)\/(.+)$ /$1 [L,R=301]

You can use:
RewriteEngine On
RewriteRule ^(.+/word2/[^/.]+)/.+$ $1 [L,R=301]

Related

301 Redirect in htaccess to remove URL Parameter? And redirect to original URL

I have following pattern of URLs in some of my WordPress site
abc.com/xolo-lt900-price-in-india.html? m=1
and
abc.com/xolo-lt900-price-in-india.html? m=0
I want to remove part which is after parameter value and 301 redirect it to main/original URL.
So,
bc.com/xolo-lt900-price-in-india.html?m=1
Shall 301 redirect to
abc.comprice4india.co.in/xolo-lt900-price-in-india.html
And in same way
abc.com/xolo-lt900-price-in-india.html? m=0
Shall 301 redirect to
abc.com/xolo-lt900-price-in-india.html
I have the following code but its not working.
RewriteCond %{QUERY_STRING} m [NC]
RewriteRule ^$ /? [R=301,L]
Thanksfully the old code from #Anubhava is working now..
RewriteCond %{QUERY_STRING} ^m=[01]$ [NC]
RewriteRule ^(.*)$ /$1? [R=301,L,NC]

htaccess file to remove folder, and replace underscores with dashes

I need to change the following url
http://somedomain.com/news/a_sample_news_article.html
to
http://somedomain.com/post/a-sample-news-article
I have this in my htaccess which works, but I am sure it can be improved upon - does anyone have a better solution?
RewriteEngine on
# replace underscores
RewriteRule ^(news)/([^_]*)_+(.*)$ /$1/$2-$3 [L,NC,R=302]
# redirect the directory from news to post
RewriteRule ^news/(.*)$ /post/$1 [R,L]
# remove .html from end of url
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
Any help much appreciated!
To redirect
/news/foo_bar
to
/post/foo-bar
you can use the following rule :
RewriteEngine on
# redirect "/news/foo_bar" to "/foo_bar"
RewriteRule ^news/(.+)$ /$1 [L,R]
#2 replace underscore with hypens
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /post/$1 [L,R]

.htaccess redirect loop - root URL duplicate

I'm trying to achieve the following with .htaccess:
domain.tld/ displays the content originally at domain.tld/home/
if you visit domain.tld/home/, you are redirected to domain.tld/ (to avoid duplicate content)
I tried the following, but it resulted in an infinite loop:
RewriteEngine on
RewriteRule ^$ /home/ [L]
RedirectMatch 301 ^/home/$ /
Any ideas? Thank you.
Found the solution:
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /+home/([^\?\ ]*)
RewriteRule ^ /%1 [R=301,L]
RewriteRule ^$ /home/ [L]
Based on: How to avoid duplicate content mapping urls with htaccess

remove a query string in htaccess 301 redirect

I have URL like these:
/one/two?page=1
/three/four/five?page=2
I need to remove "page=1" and redirect it to /one/two but only when "page=1" appears, not "page=2".
How can i do that?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=1$ [NC]
RewriteRule %{REQUEST_URI}? [L,R]

.htaccess 301 redirect from old url to SEO friendly

I have link like this:
www.site.com/page.php?p=1
Need to rewrite it to friendly URLs in htaccess
RewriteRule ^home$ page.php?p=1
It works but now I have two active links with the same content.
Tried to add 301 redirect from old link to new but stuck in loop. Any ideas how to fix that?
Try matching against the actual request so that your rules won't loop:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page\.php\?p=1(&|\ |^)([^\ ]*)
RewriteRule ^page\.php$ /home?%3 [L,R=301]
# then your internal rewrite
RewriteRule ^home$ page.php?p=1
Remove the redirect on the page and handle it in the htaccess.
RewriteRule ^page\.php\?p=1$ /home [L,R=301]
This will redirect to /home and stop the redirect loop you have now.
another quick & dirty way to prevent looping in these situations i've found is to add a querystring and then check for its existence in the redirect.
RewriteCond %{QUERY_STRING} ^p=1
RewriteCond %{QUERY_STRING} !foo=bar
RewriteRule ^page\.php$ /home [NC,R=301,L]
RewriteRule ^home$ page.php?p=1&foo=bar [NC,L]
found on this site: http://answers.oreilly.com/topic/542-how-to-properly-redirect-for-maximum-seo/
Redirect 301 /page.php?p=1 www.yourwebsite.com/home?p=1 RewriteRule
^home?p=1$ /page.php?p=1

Resources