Remove folder name from rewritten URLs
22,844
Try
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /gallery/$1 [L]
</IfModule>
Related videos on Youtube
Author by
user46547
Updated on September 18, 2022Comments
-
user46547 over 1 year
Consider this URL:
http://example.com/gallery/20141111-some-title
In this URL I want to hide
gallery/
:http://example.com/20141111-some-title
How can I achieve this using mod_rewrite?
I used online generators for doing the rewrite rules but none can remove a piece of text from the URL.
-
Zimmi over 9 yearsThe rewrite rule should be the other way, it is
RewriteRule model substitution
. here you add /gallery/ at the beginning... (second) -
Rilfi over 9 years@Zimmi When we enter
http://website.com/20141111-some-title
, we see the file that is inhttp://website.com/gallery/20141111-some-title
. I think it's what we want, no? -
Zimmi over 9 yearsI've understood the question the other way: the client asks for
http://website.com/gallery/20141111-some-title
and it is redirected with 301 tohttp://website.com/20141111-some-title
. With the rule in the answer, the client askshttp://website.com/20141111-some-title
and receiveshttp://website.com/gallery/20141111-some-title
without 30x redirect, and may recieve the same content for both urls. I'm sorry if I misunderstood the question. -
Rilfi over 9 years@Zimmi Yes, a complete answer would probably be to do Fabien's rewriting AND make a 301 so that the URL website.com/gallery/20141111-some-title is not accessible (and/or redirects to the one without 'gallery').
-
MrWhite over 7 years...Although the URL should be "changed" in the website itself, not using mod_rewrite/.htaccess. .htaccess should only be used to route the URL and to perhaps catch/redirect URLs that have already been indexed or linked to by third parties. If you rely on .htaccess to "fix" the URL, without first fixing it in the website then every time a user follows an internal link they will be externally redirected - which is bad for the user and the server (doubles the number of requests).
-
MrWhite over 7 yearsTaken literally, this does appear to answer the OPs question (+1), however, whether the OP really intends to do this is another matter. (Since without further internal rewrites this probably just results in a 404.)