0

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.

EmilyH
  • 90
  • 1
  • 9
  • How do those redirects fit in with your rewrite? Because it looks like you're still including `location` in the 301 redirect. Doesn't seem right. – Panama Jack Nov 30 '15 at 22:06
  • I had to add another category (furnished-apartments) and the permanent redirects are to fix those. Would those conflict with the rewrite? – EmilyH Nov 30 '15 at 22:26

2 Answers2

0

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]
GeorgeQ
  • 1,382
  • 10
  • 8
  • exactly why does he need the `QSA` flag when he's not dealing with any query strings? – Panama Jack Nov 30 '15 at 22:10
  • Doesn't work. When I try to visit domain.com/corporate-housing/central, it still gives me a 404. – EmilyH Nov 30 '15 at 22:10
  • @DavyH I suspect that you actually want it the other way around even though your question states that is should be directing from `domain.com/location/corporate-housing-SOMETHING/` to `domain.com/corporate-housing/SOMETHING/`. Please clarify – GeorgeQ Nov 30 '15 at 22:12
  • You're correct. As I stated in the op, I'm trying to make all urls like domain.com/location/corporate-housing-SOMETHING/ become domain.com/corporate-housing/SOMETHING/ – EmilyH Nov 30 '15 at 22:14
0

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.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • It's not working. I'll update the OP to show what I changed it to. – EmilyH Nov 30 '15 at 22:22
  • Is the rewrite going to a real directory? `/location/corporate-housing-downtown/` is that a real directory? – Panama Jack Nov 30 '15 at 22:27
  • So do you actually want a **redirect** then? That's what it sounds like going from the old link starting with /location. Try the update – Panama Jack Nov 30 '15 at 22:36
  • No, I want to rewrite URLs so that they appear in the "domain.com/corporate-housing/something/" way. Right now they appear as "domain.com/location/corporate-housing-something/" – EmilyH Nov 30 '15 at 22:41
  • Right, you need to change Wordpress Permalinks to use that new format. Then the rule above will also redirect any old links in the form of `domain.com/location/corporate-housing-something/` to the new URL.. You also need to update wordpress. https://codex.wordpress.org/Using_Permalinks – Panama Jack Nov 30 '15 at 22:46