0

I have a question that is a bit confusing. I'm trying to understand mod_rewrite but I am pretty confused by it.

What I'm trying to do is redirect all URLs such as /settings/account or /user/user123 to index.php. A PHP script (which I already created) uses $_SERVER['REQUEST_URI'] to break the URL down into pieces then uses include. For example it would include settings.php?page=account or user.php?uid=user123.

But if the URL is /settings.php?page=account or /user.php?uid=user123 I don't want it to be redirected through index. In other words, if the URl has a file extension, just go to that file, but if it doesn't (like /settings) go to index.php to process where it should go.

How can this be done? Thanks in advance for any suggestions!

PatomaS
  • 1,603
  • 18
  • 25
user2781234
  • 13
  • 1
  • 3

2 Answers2

0

If you don't want to use the commonplace redirect-every-url-which-has-no-corresponding-file approach, you can also try your approach. To redirect anything without dot to a common script, use:

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

You might want to exclude some URLs still, like images/ or css/ etc. Use a simple RewriteCond before the rule then:

 RewriteCond  %{REQUEST_URI}  !images/
 RewriteCond  %{REQUEST_URI}  !css/
Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
0

What you need is a RewriteCond directive with !-f and !-d patterns.

RewriteCond checks whether a user's HTTP request matches provided condition. In your case, you would want to check if a requested file (which is provided in a variable %{REQUEST_FILENAME}) already exists on your document root using the !-f postfix. Similarly, if you want indexing in directories, you can add the same pattern with !-d, which will check if requested directory is on your document root.

So these two lines before the actual RewriteRule would do the trick:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
NikitaBaksalyar
  • 2,414
  • 1
  • 24
  • 27