1

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.

1

Try

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /gallery/$1 [L]
</IfModule>
  • The rewrite rule should be the other way, it is RewriteRule model substitution. here you add /gallery/ at the beginning... (second) – Zimmi Nov 24 '14 at 11:12
  • 1
    @Zimmi When we enter http://website.com/20141111-some-title, we see the file that is in http://website.com/gallery/20141111-some-title. I think it's what we want, no? – caCtus Nov 24 '14 at 11:21
  • I've understood the question the other way: the client asks for http://website.com/gallery/20141111-some-title and it is redirected with 301 to http://website.com/20141111-some-title. With the rule in the answer, the client asks http://website.com/20141111-some-title and receives http://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. – Zimmi Nov 24 '14 at 11:35
  • 2
    @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'). – caCtus Nov 24 '14 at 12:31
  • ...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 Aug 14 '16 at 10:53
0

Try:

RewriteEngine on
RewriteRule ^gallery/(.+)$ http://example.com/$2 [R=301,L]
  • Taken 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.) – MrWhite Aug 14 '16 at 10:59
0

Try this:

  <IfModule mod_rewrite.c>
  RewriteEngine On 
  RewriteCond %{THE_REQUEST} ^GET\ /gallery/
  RewriteRule ^gallery/(.*) gallery/20141111-some-title$1 [L,R=301]

I hope this will works for you.

0

Try this into .htaccess it's work for me

#testing
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]

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.