0

I currently use:

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME}.php -f 
   RewriteRule ^(.+)$ $1.php [L,QSA]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME}.html -f 
   RewriteRule ^(.+)$ $1.html [L,QSA]

for removing .php and .html.

But I was wondering how would I rewrite ?[variableName]=[variable].

So for example, currently this happens:

http://myurl.com/about.html to http://myurl.com/about/

But how would I then rewrite variables such as:

http://myurl.com/foo/?bar=true to http://myurl.com/foo/bar/true/

or is this not possible?

Thanks for your help, any optimizations on my current rewrite is also welcome!

EDIT: Hi All, so after some help from zx, I have gotten to this:

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME}.php -f 
   RewriteRule ^(.+)$ $1.php [L,QSA]

   RewriteCond %{QUERY_STRING} ([^=]+)=([^=]+)
   RewriteRule ^/?$ %1/%2?  [L,R]

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME}.html -f
   RewriteRule ^(.+)$ $1.html [L,QSA]

And I link like: href="./?do=foo" and the return is /?do=foo not /do/foo/.

Here is a pastebin to 100% of the .htaccess code, including the stuff in there which was default from the host provider: click

  • possible duplicate of [Rewriting an arbitrary number of path segments to query parameters](http://stackoverflow.com/questions/3655893/rewriting-an-arbitrary-number-of-path-segments-to-query-parameters) – Sumurai8 Jun 29 '14 at 09:06
  • @Sumurai8 - Although you have flagged this question for being a duplicate, it seems they have an understanding of regex. Their way of writing the answer and explaining it DOESN'T help me at all. Thats why I created this... Hoping for a basic, decent answer. – user3580557 Jun 29 '14 at 09:26
  • If you ask a regex-related question, you are expected to have a basic understanding of regex. If you don't, then go get a basic understanding of regex. The regex used in mod_rewrite is similar to PCRE and what PHP uses. – Sumurai8 Jun 29 '14 at 09:30

1 Answers1

0

I'm getting confused about which way we're going, so here are two options.

Option 1

Link entered by user: something?do=foo

Rewritten link: do/foo

RewriteCond %{QUERY_STRING} ([^=]+)=([^=]+)
RewriteRule ^/?$ %1/%2?  [L,R]

Option 2

Link entered by user: foo/bar/true

Rewritten link: foo/bar=true

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1?$2=$3 [QSA,DPI,L,R]
  • The ^ anchor asserts that we are at the beginning of the string
  • Each ([^/]+) matches a path fragment and captures it to one of the Groups
  • /? matches an optional final slash
  • The $ anchor asserts that we are at the end of the string
  • $1?$2=$3 uses the Group captures to rewrite the url
  • QSA appends any pre-existing paremeters
  • DPI discards any path fragments
  • L tells mod-rewrite to stop examining rules after this one
anubhava
  • 761,203
  • 64
  • 569
  • 643
zx81
  • 41,100
  • 9
  • 89
  • 105