Redirect /mobile/REQUEST_URI to /REQUEST_URI - .htaccess

This is what I currently have
RedirectMatch 301 ^/mobile/ https://www.websitename.com/?
RedirectMatch 301 ^/m/ https://www.websitename.com/?
Im trying to redirect if user types /m/testpage.php to /testpage.php
the above example always redirects to same url.

Found the answer:
RewriteRule ^mobile/(.*)$ /$1 [R=301,NC,L]
RewriteRule ^m/(.*)$ /$1 [R=301,NC,L]

Related

How do 301 redirect with .htaccess?

I have url
/en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage%E2%80%8B
and I try
Redirect 301 /en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage\%E2\%80\%8B /en
or
RewriteCond %{THE_REQUEST} ^\w+ /en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage%E2%80%8B
RewriteRule ^ /en [R=301,L]
or
Redirect 301 /en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage\%E2\%80\%8B /en
But any version don't work. How I can redirect from this url?
You can use this rule as your first rule in site root .htaccess:
RewriteEngine On
RewriteRule ^en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage\xE2\x80\x8B/?$ /en? [L,NC,R=301]
\xMN is used in RewriteRule pattern to match %MN in URL.

301 redirect from multiple url parameters

I'm trying to redirect any of these formats
mydomain.com/?main_page=products_new
mydomain.com/?main_page=products_new&page=2
mydomain.com/?main_page=products_new&disp_order=2
to this
mydomain.com/new-products
I have this working for only the first parameter. How can I ignore the remaining ones and match on anything that contains ?main_page-products_new ?
RewriteCond %{QUERY_STRING} ^main_page=products_new [NC]
RewriteRule ^(.*) /new-products? [L,R=301]
Apache supports RedirectMatch
RedirectMatch 301 ^main_page=products_new.*$ /new-products?$1

Redirect 301 specific pages and the rest to just one page

First I need to redirect these pages to another page in a different domain
Redirect 301 /example1 http://newdomain.com/test1
Redirect 301 /example2 http://newdomain.com/random1
Note the pages are not the same in the new domain (e.g., /example1 to /test1)
After that, I need redirect the rest of the pages to newdomain.com
E.g., Redirect 301 (everything else) to http://newdomain.com
Try below rule, using mod rewrite I am assuming you have mod rewrite enabled.
RewriteEngine On
RewriteRule ^example1$ http://newdomain.com/test1 [R=301,L]
RewriteRule ^example2$ http://newdomain.com/random1 [R=301,L]
RewriteCond %{REQUEST_URI} !^(example1|example2)
RewriteRule ^ http://newdomain.com [R=301,L]
If you want to use mod-alias , you can use these redirects :
RedirectMatch 301 ^/example1/?$ http://example.com/test1
RedirectMatch 301 ^/example2/?$ http://example.com/random1
#redirect everything else to the homepage of example.com
RedirectMatch ^.+$ http://example.com/
Clear your browser cache before testing these redirects.
Try this in your .htaccess
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

I need to create a 301 redirect in .htaccess for both a different domain and different directory structure

I tried it using RedirectMatch but my code is not working. My syntax is probably incorrect.
Here's my code:
RedirectMatch 301 ^oldsite.com/guides/$ http://newsite.com/destinations/city-guides/
RedirectMatch 301 ^oldsite.com/show/$ http://newsite.com/tv-show/concept/
RedirectMatch 301 ^oldsite.com/destinations/$ http://newsite.com/tv-show/concept/
RedirectMatch 301 ^oldsite.com/concept.html http://newsite.com/tv-show/concept/
RedirectMatch 301 ^oldsite.com/format.html http://newsite.com/tv-show/format/
RedirectMatch 301 ^oldsite.com/about.html http://newsite.com/about/
RedirectMatch 301 ^oldsite.com/contact.html http://newsite.com/contact/
The first three lines I want any file in those old directories to go to the root of the new directory. The last four are specific page to page redirects.
What am I doing wrong?
You don't match domain name in RedirectMatch. Only request URI is matched.
Try this on old site's .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?oldsite\.com$ [NC]
RewriteRule ^ - [L]
RewriteRule ^guides/$ http://newsite.com/destinations/city-guides/ [L,R=301]
RewriteRule ^show/$ http://newsite.com/tv-show/concept/ [L,R=301]
RewriteRule ^destinations/$ http://newsite.com/tv-show/concept/ [L,R=301]
RewriteRule ^concept.html http://newsite.com/tv-show/concept/ [L,R=301]
RewriteRule ^format.html http://newsite.com/tv-show/format/ [L,R=301]
RewriteRule ^about.html http://newsite.com/about/ [L,R=301]
RewriteRule ^contact.html http://newsite.com/contact/ [L,R=301]

Basic 301 rediret not working

I am trying to redirect a handful of old blog posts to new ones. Here is my htaccess that I have written and placed in the root directory. Keep in mind the site is now on linux server and not windows.
redirect 301 /public/recipesdetl.aspx?id=1888 http://www.mysite.com/recipes/
redirect 301 /public/recipesdetl.aspx?id=2025 http://www.mysite.com/recipes/?p=405
redirect 301 /public/recipesdetl.aspx?id=2682 http://www.mysite.com/recipes/?p=549
redirect 301 /public/recipesdetl.aspx?id=2152 http://www.mysite.com/recipes/?p=309
redirect 301 /public/recipesdetl.aspx?id=1398 http://www.mysite.com/recipes/?p=817
redirect 301 /public/recipesdetl.aspx?id=1908 http://www.mysite.com/recipes/?p=155
redirect 301 /public/recipesdetl.aspx?id=2162 http://www.mysite.com/recipes/?p=317
redirect 301 /public/recipesdetl.aspx?id=1889 http://www.mysite.com/recipes/?p=145
redirect 301 /public/recipesdetl.aspx?id=1951 http://www.mysite.com/recipes/?p=808
redirect 301 /public/recipesdetl.aspx?id=2032 http://www.mysite.com/recipes/?p=238
redirect 301 /public/recipesdetl.aspx?id=2101 http://www.mysite.com/recipes/?p=269
redirect 301 /public/recipesdetl.aspx?id=2147 http://www.mysite.com/recipes/?p=304
redirect 301 /public/recipesdetl.aspx?id=2563 http://www.mysite.com/recipes/?p=449
redirect 301 /public/recipesdetl.aspx?id=2043 http://www.mysite.com/recipes/?p=243
redirect 301 /public/recipesdetl.aspx?id=2224 http://www.mysite.com/recipes/?p=361
redirect 301 /public/recipesdetl.aspx?id=2151 http://www.mysite.com/recipes/?p=308
You can include query strings in the first URI-path in a Redirect statement, you'll need to use mod_rewrite's %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=1888$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/ [L,R=301]
RewriteCond %{QUERY_STRING} ^id=2025$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/?p=405 [L,R=301]
RewriteCond %{QUERY_STRING} ^id=2682$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/?p=549 [L,R=301]
RewriteCond %{QUERY_STRING} ^id=2152$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/?p=309 [L,R=301]
etc.

Resources