Trying to get
https://example.com/es/[path]
to go directly to
https://example.com/[path]
I have tried multiple bits of code and none work.
What I've tried:
RewriteEngine on
RewriteRule ^/es/$ /$1 [L,R=301]
The above is instead redirecting me to the home page always. What am I doing wrong?
You have some issues with your current implementation which is why I doubt that this rule you posted actually "redirecting to the home page":
The matching pattern inside a RewriteRule
is matched against the relative path of the requested URL. That is documented. Your pattern ^/es/$
will never match a relative path due to the leading slash.
You don't catch anything in that pattern which is why the $1
will never hold anything in the target definition.
The pattern only matches the exact string /es/
, not something that contains anything longer like /es/[path]
due to the usage of the $
anchoring the pattern to the subjects end.
Try that instead:
RewriteEngine on
RewriteRule ^/?es/(.*)$ /$1 [L,R=301]
You can implement such rule in the central configuration of your http server's virtual host setup. If you do not have access to that (for example if you are using a cheap hosting provider instead of operating the http server yourself) you can instead use a distributed configuration file (often called ".htaccess"), though that comes with a number of disadvantages. And you have to enable the interpretation of such files first in your http server's configuration (see the documentation for the AllowOverride
directive).
I strongly advise to take a look into the documentation of the tools you are trying to use, here the rewriting module for the apache http server. As typical for OpenSource software it is of excellent quality and it comes with helpful examples: https://httpd.apache.org/docs/current/mod/mod_rewrite.html