-2

I want this:

https://example.com/something

From this:

https://example.com/?q=something

I found many similar questions stackoverflow, but none of them worked for me, so please help me.

For example i tried this:

RewriteEngine On
RewriteRule ^(.*) index\.php?q=$1

with this php code:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Document</title>
  </head>
  <body>
    <?php echo $_GET['q'] ?>
  </body>
</html>

but it's always shows "index.php"

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Levente
  • 21
  • 1
  • 5
  • 2
    If none of the existing questions provide a solution (while they should, since this is a very common thing to do), please ask a question describing in detail what you have tried and what problems you are facing. – Markus AO Oct 25 '21 at 14:09
  • @MarkusAO for example i tried this: `RewriteEngine On RewriteRule ^(.*) index\.php?q=$1` but it always returns "index.php" – Levente Oct 25 '21 at 14:12
  • Please update your question with the details of what you have tried. – Markus AO Oct 25 '21 at 14:13
  • 1
    It sounds like you have a different rewrite somewhere else, which is running _before_ this one, so that the URL is being rewritten twice: `/something` -> `/index.php`, and then `/index.php` -> `/index.php?q=index.php` To test that theory, try making a page called something else, like `test.php`, and changing your rule to `RewriteRule ^(.*) test\.php?q=$1` – IMSoP Oct 25 '21 at 14:24
  • You could also try enabling logging for the rewrite module, and posting the results of that in your question. [Instructions for Apache 2.4 are here](http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging), or if those don't work try [the version for 2.2 here](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog). – IMSoP Oct 25 '21 at 14:27
  • The rule you posted will _roughtly_ do the opposite of what you ask. – arkascha Oct 25 '21 at 18:03
  • Relevant: https://stackoverflow.com/q/66942536/2943403 and https://stackoverflow.com/q/20563772/2943403 and https://stackoverflow.com/q/7677070/2943403 – mickmackusa Oct 26 '21 at 01:44

1 Answers1

0
RewriteRule ^(.*) index\.php?q=$1

...but it's always shows "index.php"

That's because the rather generic pattern ^(.*) also matches index.php and ends up rewriting the request to index.php?q=index.php on the second pass of the rewrite engine (when used in a directory context, like .htaccess).

If your URL's (like /something) don't contain dots then you could simply exclude dots from the regex, so it won't match index.php. Excluding dots also means it should avoid matching any static resources (that usually map directly to files that end in a file extension, which are naturally delimited by a dot, eg. image.jpg, styles.css, etc.).

For example, try the following instead:

RewriteRule ^([^.]*)$ index.php?q=$1 [L]

There's no need to backslash-escape the literal dot in the substitution string (2nd argument) since this is a "regular" string, not a regex, and the dot carries no special meaning here.

MrWhite
  • 43,179
  • 8
  • 60
  • 84