301 permanent redirection for various depth - .htaccess

I use this code for R301 for 1 depth of category.For example I want redirect http://sitesample.ltd/new/mynews to http://sitesample.ltd/new/this is my code:
RewriteRule ^mynews/(.+)$ http://sitesample.ltd/new/$1 [R=301,L]
And I want remove depth 2 and 3 from url. For example redirect http://sitesample.ltd/new/mynews/photo to http://sitesample.ltd/new/.I try this but not work:
RewriteRule ^mynews/(.+)/?$ http://sitesample.ltd/new/$2 [R=301,L]
RewriteRule ^mynews/(.+)/(.+)/?$ http://sitesample.ltd/new/$3 [R=301,L]
Another try but no luck:
RewriteRule ^mynews/([^/]+)/?$ http://sitesample.ltd/new/$2 [R=301,L]
RewriteRule ^mynews/(.+)/([^/]+)/?$ http://sitesample.ltd/new/$3 [R=301,L]

If you try to do that:
mynews/xxxx -> http://sitesample.ltd/new/xxxx
mynews/yyyy/xxxx -> http://sitesample.ltd/new/xxxx
mynews/zzzz/yyyy/xxxx -> http://sitesample.ltd/new/xxxx
You can do that:
RewriteRule ^mynews/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
RewriteRule ^mynews/[^/]+/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
RewriteRule ^mynews/[^/]+/[^/]+/([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]
Or only this:
RewriteRule ^mynews/(?:[^/]+/){0,2}([^/]+)/?$ http://sitesample.ltd/new/$1 [R=301,L]

Related

htaccess rewrite - Hide subfolder from URL

We have a page file here:
ourdomain.com/subfolder1/subfolder2/
But we'd like people to be able to access it by going here:
ourdomain.com/subfolder2/
So we need both a redirect and a rewrite.
Any idea how this can be achieved using htaccess? I've tried a million things and nothing is properly rewriting/redirecting.
What I currently have...
RewriteCond %{REQUEST_URI} ^/subfolder1/subfolder2/?$ [NC,OR]
RewriteRule ^(.*) http://www.ourdomain.com/subfolder2/ [R=301,L]
You can use:
# Redirect from /subfolder1/subfolder2/...
RewriteCond %{THE_REQUEST} \s/+subfolder1/(subfolder2/[^\s?]*) [NC]
RewriteRule ^ %1 [R=301,L]
# Rewrite to /subfolder1/subfolder2/...
RewriteRule ^(subfolder2/.*) /subfolder1/$1 [NC,L]

htaccess redirection subdomain

I want to redirect feedback.domain.de to www.domain.de/de/abc/cde.html via htaccess.
My current htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
SO, I thought just to add:
redirect 301 feedback.domain.de www.domain.de/de/abc/cde.html
but it doesn't work. If i try to open feedback.domain.de it redirects to www.domain.de
I know this is a very easy question but I don't how solve it in htaccess :-(
The result I want is:
domain.de -> www.domain.de/de/index.html
www.domain.de -> www.domain.de/de/index.html
domain.de/de/example.html -> www.domain.de/de/example.html
etc...
feedback.domain.de -> www.domain.de/de/feed.html
Best regards
The Redirect directive only accept a path relative to the root path (for example /my-path) and won't match on the host part of the URL.
Try this (note that here I assume you want to redirect domain.de to www.domain.de and not all subdomains):
RewriteEngine On
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.domain\.de$
RewriteRule . http://www.domain.de/de/abc/cde.html [L,R=301]
# Redirect domain.de to www.domain.de
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule . http://www.domain.de/$0 [L,R=301]
Updated answer (2013-03-18):
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.(domain\.(de))$
RewriteRule .+ http://www.%1/%2/feed.html [L,R=301]
# domain.de -> www.domain.de/de
RewriteCond %{HTTP_HOST} ^(domain\.(de))$
RewriteRule .+ http://www.%1/%2/$0 [L,R=301]
# www.domain.de/de -> www.domain.de/de/index.html
RewriteCond %{HTTP_HOST} ^www\.domain\.(de)$
RewriteRule ^%1/?$ index.html [L,R=301]

Remove query string - rewrite rule

How do i remove a query string from the end of a url?
To be more specific, this is my rewrite rule:
RewriteRule example-(.*).html$ examples/view-example.php?param1=parameter&param2=$1&split=-
and I want this to return a 404 or a redirect to www.mydomain.com/example-one.html :
www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-
This is what i tried, it doesn't work:
RewriteCond %{REQUEST_URI} /examples/view-example\.php
RewriteCond %{QUERY_STRING} param1=parameter&param2=(.*)&split=-
RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$
I think that RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$ isn't correct..
This should do the trick:
RewriteCond %{QUERY_STRING} ^param1=parameter&param2=(.*)&split=-
RewriteRule ^/examples/view-example\.php$ http://mydomain.com/example-%1.html [R=301]
Though, I didn't understand what you wanted to do with www.mydomain.com/example-one.html?param1=parameter&param2=one&split=-

htaccess redirect with on rule

how to write one rule what would satisfy all this redirect what I try to achive with the following rules
RewriteRule ^desktop/page/1/ /tag/desktop/page/1/ [R,L]
RewriteRule ^desktop/page/2/ /tag/desktop/page/2/ [R,L]
RewriteRule ^desktop/page/3/ /tag/desktop/page/3/ [R,L]
RewriteRule ^desktop /tag/desktop [R,L]
the last one is never executed
Try:
RewriteRule ^desktop(/.*)?$ /tag/desktop$1 [R,L]
Or if you must limit it to page/number:
RewriteRule ^desktop(/(page/[0-9]+/?)?)?$ /tag/desktop$1 [R,L]

How do i do a 301 redirect to a specific location

I want to remove the slash of 1 and only 1 url
this snippet will remove them all
# Remove the trailing slash
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
but i just want to change
example.com/changeme/
to
example.com/changeme
any ideas how to change this htaccess i have to only do it on one
You could replace (.+) with (changeme) in RewriteRule:
RewriteRule ^(changeme)/$ http://www.example.com/$1 [R=301,L]
This will only match 'changeme' and not everything.
In this way you can also match multiple URLs, including e.g. 'changeother' and 'foobar':
RewriteRule ^(changeme|changeother|foobar)/$ http://www.example.com/$1 [R=301,L]
How about this?
RewriteRule ^changeme/$ http://www.example.com/changeme [R=301,L]
All you need is (as long as this rewrite is only applied to example.com):
RewriteRule ^changeme/$ changeme [R=301,L]

Resources