Mobile app version of vmapp.org
Login or Join
Si4351233

: Htaccess w/ Wordpress: rewrite specific path to root domain but keep full path I want http://example.com/foo-bar to display the webpage from http://example.com/ but retain /foo-bar in the address

@Si4351233

Posted in: #Htaccess #ModRewrite

I want
example.com/foo-bar

to display the webpage from
example.com/

but retain /foo-bar in the address bar. I know it appears as a duplicate of many RewriteRule questions asked here, but none of which I tried retained the first URL in the address bar; they were simple 301/302 redirects.

What I have in .htaccess right now:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(example.com)$
RewriteRule ^foo-bar example.com [NC,L]

# 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


Is this only possible with proxies or am I missing something?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Si4351233

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

As you suspect, you need to internally rewrite the request, not externally redirect (301/302). No "proxies" are required. You are almost there with what you have, except that when you specify an absolute URL in the RewriteRule substitution Apache will implicitly trigger an external redirect (despite the docs suggesting that if the domain matches the current host it should be stripped - it doesn't do this in .htaccess in my experience).

Try something like the following:

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^foo-bar$ index.html [L]


Unless you are serving mutliple domains then you probably don't need the RewriteCond directive? (The NC flag here is simply a security measure to catch malformed requests.)

I appended a $ (end of string) anchor on the RewriteRule pattern to make it an exact match for "foo-bar" and not a URL that starts "foo-bar". The substitution should reference your DirectoryIndex document (index.html, index.php, or whatever document is loaded by default in the document root). I've removed the NC flag, unless you specifically need a case-insensitive match?


UPDATE: There actually are some Wordpress ...


Ah, WordPress is the "problem" here. In fact, WP already rewrites all requests through index.php in the document root, so these new directives (to "rewrite specific path to root domain but keep full path") are entirely redundant.

WP then uses the URL (the visible path in the address bar) ie. "/foo-bar" in this case, to route the request. Unless "/foo-bar" is defined as a valid URL in WP itself then WP is going to generate a 404 Not Found.

This is something you need to configure in WP itself, not .htaccess.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme