2

I want

http://example.com/foo-bar

to display the webpage from

http://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 http://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?

2

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.

  • Unfortunately, replacing the last two lines with the above code removed the redirect altogether. – Doruk Karınca Aug 4 '16 at 16:56
  • Try replacing the RewriteRule substitution (a single slash) with the actual directory index document (eg. index.php or index.html, etc.) - I've updated my answer. Also, remove the RewriteCond directive altogether if it's not required, as mentioned above - this is only required if you are hosting multiple domains. These directives should go before any other internal rewrites, but probably after any other external redirects in your .htaccess file - the order to directives in the Apache config file are important. – MrWhite Aug 4 '16 at 18:07
  • That changed nothing either. There actually are some Wordpress defaults in the htaccess as I edited my question but adding your code before or after the if module doesn't affect the outcome. – Doruk Karınca Aug 4 '16 at 20:47
  • 1
    Unfortunately WordPress is the "problem" - I've updated my answer. "...the above code removed the redirect altogether." - what did you actually mean by this? I assume you are seeing a WordPress generated 404 page? – MrWhite Aug 4 '16 at 23:29
  • 1
    ...and not rewrite /foo-bar at all? What page are you wanting to display at /foo-bar? Presumably you want the WordPress home page located at the URL http://example.com/ to display? In order to display a WP page (that's stored in the database) you need to go through index.php, which means rewriting the URL at some point (or redirecting) and configuring WP to understand /foo-bar. If, however, you wanted to display an independent webpage, outside of WP, then yes you could write an exception into the WP code block - although you probably wouldn't need to do this. – MrWhite Aug 5 '16 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.