skip to Main Content

I want to rewrite

http://site.tld/fake1/fake2/page?options

to

http://site.tld/page?options

basically getting everything is after the last slash, so without knowing in advance any fake foder names

2

Answers


  1. You can use capturing groups and replace as follows.

    Regex: ^(.*.tld/).*?([^/]*$)$

    Explanation:

    • ^(.*.tld/) matches beginning part till domain name.

    • .*? matches the fake1/fake2/

    • ([^/]*$)$ matches the later part of URL.

    Replacement to do: Replace with back-referencing groups 12.

    Regex101 Demo

    Login or Signup to reply.
  2. First, make sure you’ve enabled mod_rewrite and allowed htaccess in Apache configuration file.

    Then, put this code in your htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^.+?/([^/]+)$ /$1 [L,QSA]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search