0

This is my current htaccess

<IfModule mod_rewrite.c>
    # turn on rewrite engine
    RewriteEngine on

    # if request is a directory, make sure it ends with a slash
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*/[^/]+)$ $1/

    # if not rewritten before, AND requested file is wikka.php
    # turn request into a query for a default (unspecified) page
    RewriteCond %{QUERY_STRING} !wakka=
    RewriteCond %{REQUEST_FILENAME} wikka.php
    RewriteRule ^(.*)$ wikka.php?wakka= [QSA,L]

    # if not rewritten before, AND requested file is a page name
    # turn request into a query for that page name for wikka.php
    RewriteRule ^(.*)$ wikka.php?wakka=$1 [QSA,L]

</IfModule>

My current url structure is

www.domain.com/site/pool/Page_Example_Test
www.domain.com/site/pool/Page_Example_Test/edit
www.domain.com/site/pool/Page_Example_Test/edit?id=1
www.domain.com/site/pool/Page_Example_Test/history
www.domain.com/site/pool/Page_Number_Room
www.domain.com/site/pool/Page_Number_Room/edit

How Is it possible to access them like this

www.domain.com/site/pool/Page/Example/Test
www.domain.com/site/pool/Page/Example/Test/edit
www.domain.com/site/pool/Page/Example/Test/edit?id=1
www.domain.com/site/pool/Page/Example/Test/history
www.domain.com/site/pool/Page/Number/Room
www.domain.com/site/pool/Page/Number/Room/edit

having the htaccess change only those "/" for "_" There is only /history and /edit finishing the url name nothing more, the normal is without /edit or /history.

flynn
  • 37
  • 7

2 Answers2

1

If there are between one and five pieces in the page name, such as

Page
Page/Page
Page/Page/Page
Page/Page/Page/Page
Page/Page/Page/Page/Page

and every element starts with a capital letter and every other part of the url is only lowercase, then you can accomplish this with four replacements (the one-piece-only one needs no replacement). For example, for three pieces:

  • Find what: ([A-Z][a-z]+)\/([A-Z][a-z]+)\/([A-Z][a-z]+)(\/)?
  • Replace with: $1_$2_$3$4

You can try it here (although I don't understand why it's only replacing with spaces in every line but the last).

Notes:

  • Each part (...) is captured
  • The final slash \/? is optional, and is the slash before the potential "edit" or "history".
  • In some regex flavors, you don't need to escape the slash /, but it's safer to do so: \/.
  • Each $[number] is a capture group reference
  • WARNING! These replacements must be done from longest to shortest: five pieces, then four, then three, then two. Otherwise, you'll seriously mess things up.

All the links in this answer come from the Stack Overflow Regular Expressions FAQ. Please consider bookmarking it for future reference. In particular, see the list of online regex testers in the bottom section, so you can try things out yourself.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

Place this code in /site/pool/.htaccess:

RewriteEngine On
RewriteBase /site/pool/

RewriteRule "^(Page)/([^/]+)/([^/]+/.*)$" /$1/$2_$3 [L]
RewriteRule "^(Page)/([^/]+)/([^/]+)$" /$1/$2_$3 [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643