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.