0

I would like every request

example.com/foo/anything

to be rewritten into example.com/foo-anything before being processed by Wordpress.

I tried this:

RewriteEngine On
RewriteRule ^foo/(.*)   foo-$1       # <-- does not work because the subs. string should be a *file path*

# The following is set by Wordpress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

but it does not work because the substitution (destination) string is supposed to be a file path, not another URL.

How to make such a RewriteRule for which the substitution string is not a file path, but an URL, that should be processed by next rules?

Notes:

  • With RewriteRule ^foo/(.*) foo-$1 [R], it works but then it causes a browser redirection and the URL changes in the URL bar of the browser, which I don't want.

  • I tried with RewriteRule flag [PT] which should work even when the substitution string is not a file path, but it did not work for me: it does not redirect /foo/anything to page /foo-anything.

  • I tried

       RewriteRule ^foo/(.*)   index.php?name=foo-$1  [L]
    

    but then it only works for articles, but not pages. I would like a solution working for both pages and articles.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • Pretty sure this is a WordPress problem, at least partially. It parses the originally requested URL, and doesn't take any _internal_ rewrites into account. (If it did, then the whole thing would not work to begin with, because then it would always just see `/index.php`, and would have no idea what content it is supposed to show based on that.) – CBroe Oct 21 '21 at 14:38
  • 1
    I think you might have more luck, if you set this up using https://developer.wordpress.org/reference/functions/add_rewrite_rule/ – CBroe Oct 21 '21 at 14:46
  • WP framework won't work with internal rewrite rules in .htaccess. You will have to use WP rewrite API inside your theme php file. – anubhava Oct 21 '21 at 14:55
  • @CBroe I've used this WP function `add_rewrite_rule` before, but I noticed what it finally does is that it rewrites the `.htaccess` file :) Also this function `add_rewrite_rule` seems to have the same limitation: the substitution string has to be a file path (such as `anything.php?query=foo`) and not another URL. The question is: how does WP have access to the *originally requested URL*? – Basj Oct 21 '21 at 16:56
  • @anubhava I've tried it before, but the same problem occured, see my other comment. But, by the way, how does WP access to the originally requested URL? – Basj Oct 21 '21 at 16:57
  • 1
    "how does WP have access to the originally requested URL?" - Presumably through the PHP superglobal `$_SERVER['REQUEST_URI']`. "I tried with RewriteRule flag `[PT]` which should work" - That only applies when used in a _server_ context. In `.htaccess` `PT` is essentially the default behaviour. Incidentally, in `.htaccess`, the subs-string is seen as a URL-path, not a file-path (particularly since `RewriteBase` is defined).. – MrWhite Oct 21 '21 at 18:07
  • @MrWhite I think you can post your comment as an answer, I'll accept it. – Basj Oct 22 '21 at 15:43

1 Answers1

0

After some research, this does not seem possible.

Either it's a RewriteRule with [R] redirection flag, and then the browser will do a new request, then in this case the target/substitution can be any new URL...

... or it's not a redirection RewriteRule and then the target/substitution has to be a file that serves the request.

See also How to use RewriteRule such that $_SERVER['REQUEST_URI'] is modified too for PHP?


In a Wordpress context, a (working) hack is to not use a .htaccess RewriteRule but do this instead in functions.php:

$_SERVER['REQUEST_URI'] = preg_replace(..., ..., $_SERVER['REQUEST_URI'])
Basj
  • 41,386
  • 99
  • 383
  • 673