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
Related
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
I am working on a WordPress website and thanks to some stubborn plugins, I need to rewrite URLs to make the structure neat. However, I am running into an odd issue and I cannot figure it out on my own.
What I'm trying to do:
rewrite all URLs like:
domain.com/location/corporate-housing-SOMETHING/
to:
domain.com/corporate-housing/SOMETHING/
If I include the full domain path, it REDIRECTS successfully. When I take away the domain name and nothing else, it gives me a 404. I've tested on multiple browsers to make sure I'm not caching anything. My .htaccess file is below:
#DirectoryIndex maintenance.html index.php index.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
Redirect 301 /location/central-west-end /location/corporate-housing-central-west-end/
Redirect 301 /location/claytonbrentwood /location/corporate-housing-claytonbrentwood/
Redirect 301 /location/downtown /location/corporate-housing-downtown/
Redirect 301 /location/west-county /location/corporate-housing-west-county/
Redirect 301 /location/central /location/corporate-housing-central/
Redirect 301 /location/st-charlesofallon /location/corporate-housing-st-charlesofallon/
Redirect 301 /location/south-county /location/corporate-housing-couth-county/
Redirect 301 /location/metro-east-illinois /location/corporate-housing-metro-east-illinois/
RewriteEngine On
RewriteBase /
RewriteRule ^corporate-housing/([a-zA-Z0-9-]+)/?$ /location/corporate-housing-$1/ [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Changed to this on recommendation, still not working:
#DirectoryIndex maintenance.html index.php index.html
# BEGIN WordPress
<IfModule mod_rewrite.c>
Redirect 301 /location/central-west-end /location/corporate-housing-central-west-end/
Redirect 301 /location/claytonbrentwood /location/corporate-housing-claytonbrentwood/
Redirect 301 /location/downtown /location/corporate-housing-downtown/
Redirect 301 /location/west-county /location/corporate-housing-west-county/
Redirect 301 /location/central /location/corporate-housing-central/
Redirect 301 /location/st-charlesofallon /location/corporate-housing-st-charlesofallon/
Redirect 301 /location/south-county /location/corporate-housing-couth-county/
Redirect 301 /location/metro-east-illinois /location/corporate-housing-metro-east-illinois/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^corporate-housing/([^/]+)/?$ /location/corporate-housing-$1/ [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Found the solution to this. I added the following to functions.php:
add_action( 'init', 'add_rewrite_rules' );
function add_rewrite_rules() {
add_rewrite_rule( 'corporate-housing/([0-9a-zA-Z-]+)/?', 'index.php?location-categories=corporate-housing-$matches[1]', 'top' );
add_rewrite_rule( 'furnished-apartments/([0-9a-zA-Z-]+)/?', 'index.php?location-categories=furnished-apartments-$matches[1]', 'top' );
flush_rewrite_rules();
}
And then commented out the flush rewrite once it had taken effect.
Based on the question your rule is incorrect. The rule should be RewriteRule {rewrite_pattern} {rewrite_target} [{flags}].
RewriteRule ^/location/corporate-housing-([a-zA-Z0-9-]+)/$ corporate-housing/$1/ [NC,L]
You need to change your code to look like this. Checks for real file or folders if doesn't exist it goes to the next rule.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^location/corporate-housing-(.+)/?$ http://www.example.com/corporate-housing/$1 [R=301,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteRule . /index.php [L]
Let me know how this works for you.
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]
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]
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/