Redirect 301 in .htaccess not accepting slash - wordpress

I've got a WordPress blog that I'm trying to redirect to a new domain. The 301 redirect works, but it redirects to newdomain.comnew-post instead of newdomain.com/new-post.
Somehow, it's stripping out the slash. Here's the code:
# 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
Redirect 301 /old-post/ http://www.newdomain.com/new-post/
What am I missing?

That shouldn't be happening unless there's also a redirect happening at the www.newdomain.com site. You need to make sure that isn't happening.
Another possibility is that maybe mod_alias (the Redirect directive) and mod_rewrite (all the rewrite rules for wordpress) may be interfering with each other. Both get applied to the same URI (when it's /old-post/ in the same URL-file mapping process pipeline. You could try removing the Redirect and stick to mod_rewrite, before wordpress' rules get applied at all. Try something like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old-post/(.*)$ http://www.newdomain.com/new-post/$1 [L,R=301]
</IfModule>
# 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

This may not be a solution for this exact scenario, but it did solve the problem where the slash character got removed from the URL it was redirecting to.
My code was similar to this:
Redirect 301 / http://www.newdomain.com
Redirect 301 /old-post/ http://www.newdomain.com/new-post/
The solution was to add a slash after the first line:
Redirect 301 / http://www.newdomain.com/
Redirect 301 /old-post/ http://www.newdomain.com/new-post/
One could also try:
Options +FollowSymlinks
RewriteEngine on
RedirectMatch 301 ^/old-post/ http://www.newdomain.com/new-post/

Related

Redirect an specific URL to another specific URL for same domain

Well I have been using a wordpress site. I want to redirect few urls. for example:
http://example.com/test To http://example.com/final/game
I tried using htaccess but got no success yet.
Try 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]
RedirectMatch 301 /test/ /final/game/
</IfModule>
# END WordPress
If that's a wordpress site then you don't have to waste your time in htaccess, you can simply use different redirect plugin to redirect your url. One of most popular plugin is here
looks like your missing the http:// from your redirect:
Redirect 301 /test/ http://example.com/final/game

htaccess 443 port added to url when using https RewriteRule & url 301 redirect

.htaccess file is performing a global 301 redirect from HTTP to HTTPS and the following code works fine
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
However I also have 30 or so Redirect 301 commands positioned below the above code and whenever they execute the resulting url has :443 incorrectly appended to the domain url and this causes issues with my current Wordpress installation.
Example :
Redirect 301 /oldDirectory/oldPage/ /newDirectory/newPage/
Incorrectly returns as :
http://www.domain.com:443/newDirectory/newPage/
Instead of :
https://www.domain.com/newDirectory/newPage/
Any suggestions or tips?
Thank you :-)
Have you tried this?
RewriteCond %{HTTP_HOST} :443$
RewriteRule ^/(.*)$ htt[b]ps:[/b]//www.example.com/$1 [R=301,L]

Conflicting HTAccess files - rewrites and 301s not working for wordpress sites

I have two sites hosted on one server. An old one in a subdirectory and a new one in the root folder.
blog.domain.com in /var/www/ - The Current Site
blog.domain.com/blog in /var/www/blog/ - The Old Site
blog.domain.com/blog is the legacy site that we need to redirect to the new one for various reasons.
I have added a load of 301s to htaccess in the /var/wwww/blog/ folder:
Redirect 301 / http://blog.domain.com
Redirect 301 /subfolder/ http://blog.domain.com
However when I visit htttp://blog.domain.com/blog I get redirected to http://blog.domain.comblog instead of: http://blog.domain.com
Here are the existing HTAccess files:
/var/www/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /domain/
RewriteCond %{HTTP_HOST} ^78\.41\.157\.192$
RewriteRule ^(.*)$ http://blog.domain.co.uk/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
/var/www/blog/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
Redirect 301 / http://blog.domain.com
Redirect 301 /subfolder/ http://blog.domain.com
I did not write the htaccess files, any insight into any errors or why adding 301s wouldnt work would be great. I presume its due to a rewrite rule.
The reason this is happening is you're missing a / at the end of your redirect destination. Apache's Redirect simply matches the target URI then appends anything after it to the destination. So:
Redirect 301 / http://blog.domain.com/
Redirect 301 /subfolder/ http://blog.domain.com/
However, since you have wordpress routing rules, you're not going to want to use Redirect at all. Redirect is part of mod_alias, not mod_rewrite, which means when a requested URL matches both modules, they both get applied. So you'll get some weird unanticipated redirection. Try using mod_rewrite only, and add these rules before your wordpress rules:
RewriteRule ^$ http://blog.domain.com/ [L,R=301]
RewriteRule ^subfolder/?$ http://blog.domain.com/ [L,R=301]

wordpress: htaccess redirect url

I want to edit Wordpress 3.5 .htaccess file to define some url redirect rules eg:
Redirect http://localhost/my_site/blog/cat/hello-world to http://localhost/my_site/cat/hello-world
I tried
Redirect 301 /localhost/my_site/blog/cat/hello-world/ http://localhost/my_site/hello-world/
and also
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_site/
Redirect 301 http://localhost/my_site/blog/cat/hello-world/ http://localhost/my_site/cat/hello-world/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my_site/index.php [L]
</IfModule>
but browser says
Page Not Found
This is somewhat embarrassing, isn’t it?
any idea.. how can i do this..?
You don't want to use Redirect in conjunction with mod_rewrite, just stick with mod_rewrite. Replace the Redirect with:
RewriteRule ^my_site/blog/cat/hello-world/ /my_site/cat/hello-world/ [L,R=301]

.htaccess 301 redirect

I switched my website from MODx to Wordpress to a different platform so now I have to make a 301 redirect.
All I need to do is to 301 Redirect http://www.domain.com/page-name.html to http://www.domain.com/page-name/.
What lines should I add to my Wordpress .htaccess file?
.htaccess file
# 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
For just one page, try:
Redirect 301 /page-name.html /page-name/
For everything ending with html, try:
RedirectMatch 301 ^/([^.]+)\.html$ /$1/
Be aware the full URL does not work, only use the relative URL

Resources