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.
Related
Just moved my website from www.example.com/blog to www.example.com.
I need to add a 301 redirect to my .htaccess to redirect everything that is called with the www.example.com/blog/... url, like a www.example.com/blog/category to www.example.com/category or www.example.com/blog/post-title to www.example.com/post-title.
I've redirected the folder but it doesn't seem to work. Can you help me?
This is the code with the 301 of the folder:
RewriteEngine On
RewriteRule ^blog(/.*)?$ /$1 [L,NC,NE,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^blog(/.*)?$ /$1 [L,NC,NE,R=301]
RewriteRule ^index\.php$ - [L]
RewriteRule . /index.php [L]
Inside your site root .htaccess have this rule at the top:
RewriteEngine On
RewriteRule ^blog(/.*)?$ https://%{HTTP_HOST}/$1 [L,NC,NE,R=301]
.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]
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/
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