How to redirect default permalink with htaccess on wordpress? - .htaccess

How to redirect default permalink to new domain with /%postname%/ permalink by .htaccess? I already try all case with Redirect and RedirectMatch but didn't work.
Redirect 301 /?p=123 http://newdomain.com/new-post
Redirect 301 http://olddomain.com/?p=123 http://newdomain.com/new-post
RedirectMatch 301 /?p=123 http://newdomain.com/new-post
RedirectMatch 301 http://olddomain.com/?p=123 http://newdomain.com/new-post

Try
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]

Related

301 Redirect subpages to new main page

Hi I am new to redirects.
I can get the basic Redirect 301 /page1/ https://example.com/newpage/ redirects to work fine.
I am having an issue with removing a parent page and all of it's child pages and redirecting everything to a new parent page.
The main redirect works:
Redirect 301 /eye-can-hear/ htps://example.com/procedures-services/
But trying to redirect the sub pages
Redirect 301 /eye-can-hear/hearing-services/ https://example.com/procedures-services/
Is redirecting to /procedures-services/hearing-services/ which doesn't exist.
Here is what the .htaccess file looks like, the 2-7 do not redirect properly:
Redirect 301 /eye-can-hear/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/hearing-services/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/hearing-aids/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/self-test-your-hearing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/schedule-your-hearing-evaluation/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/financing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/dual-sensory-wellness/ https://example.com/procedures-services/
Redirect 301 /about-costello-eye-physicians/community-involvement/ https://example.com/about-costello-eye-physicians/in-the-news/
There are some more redirect's but I can only post 7 links. Further down the .htaccess file after some iThemes security stuff is this:
# Stop wordpress username enumeration vulnerability
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^/?author=([0-9]*)
RewriteRule ^(.*)$ https://example.com/? [L,R=301]
and also this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Change this lines (2-7) that do not redirect properly to this rewrite:
RewriteEngine On
RewriteRule ^eye-can-hear\/(.*)$ https://example.com/procedures-services [R=301,L]
It's redirect all subpages of /eye-can-hear/* to https://example.com/procedures-services
This line of code worked for me in the .htaccess file:
RedirectMatch 301 /eye-can-hear/(.*) https://example.com/procedures-services
It redirects the page https://example.com/eye-can-hear/ and its subpages to the URL https://example.com/procedures-services
(Answer after two years, but maybe it helps someone)
In the htaccess file, you should put this redirection after all other redirections of subpages:
Redirect 301 /eye-can-hear/ https://example.com/procedures-services/
Your htaccess should be like this:
Redirect 301 /eye-can-hear/hearing-services/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/hearing-aids/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/self-test-your-hearing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/schedule-your-hearing-evaluation/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/financing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/dual-sensory-wellness/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/ https://example.com/procedures-services/

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]

htaccess 301 redirect with question mark

I have list of URL (600 approximately) that needs to be redirected, few URL are:
Redirect 301 /assets?/elfinder1?/elfinder.html http://www.example.com/swiftkanban/
Redirect 301 /backup?/elfinder?/elfinder.html http://www.example.com/swiftkanban/
Redirect 301 /blog?/xmlrpc.php http://www.example.com/blog/
Redirect 301 /blog.feed?type=rss http://www.example.com/blog/
Redirect 301 /m?/ http://www.example.com/swiftkanban/
Redirect 301 /m/ http://www.example.com/swiftkanban/
Redirect 301 /mobile?/ http://www.example.com/swiftkanban/
Redirect 301 /mobile/ http://www.example.com/swiftkanban/
Redirect 301 /SgjRY?/blog?/16-david-blog?/63-who-owns-kanban.html http://www.example.com/blog/who-owns-kanban/
Redirect 301 /support?/about.html http://www.example.com/about-us/
Redirect 301 /support?/about_us http://www.example.com/about-us/
Redirect 301 /support?/aboutus http://www.example.com/about-us/
Redirect 301 /support?/company http://www.example.com/about-us/the-team/
Redirect 301 /support?/contact.html http://www.example.com/contact-us/
Redirect 301 /support?/contact_us http://www.example.com/contact-us/
Redirect 301 /support?/contactus http://www.example.com/contact-us/
The problem is, I cannot do wildcard redirect, and this redirect rule does not work, I tried escaping the question mark using \ but this does not seem to work, can someone point me to the right direction?
Redirect directive cannot match query string. You need to use mod_rewrite based rules and use a RewriteCond directive as example below:
RewriteEngine On
RewriteCond %{THE_REQUEST} /blog\?/xmlrpc\.php [NC]
RewriteRule ^ http://www.example.com/blog/? [L,NE,R=301]
RewriteCond %{THE_REQUEST} /blog\.feed\?type=rss [NC]
RewriteRule ^ http://www.example.com/blog/? [L,NE,R=301]
Trailing ? after /blog/ is for removing pre-existing query string. If you want query string after redirect then remove ? from target URL.
References:
Apache mod_rewrite Introduction
.htaccess tips and tricks

htaccess 301 redirect not working with my site

I have rewrite URL via htaccess now I want to redirect that URL to another URL.
Now I want to redirect http://www.domain.com/tag/example-page URL to http://www.domain.com/tag/example
redirect 301 /tag/example-page http://www.domain.com/tag/example
Please help me regarding this issue.
Below is my rewrite rule, which I am using for short URL
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Keep this rule as your very first rule in root .htaccess:
RedirectMatch 301 ^/tag/example-page/?$ /tag/example

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