0
votes

Can anyone help me please with .htaccess?

I have a folder system on my site:

  • user

    • index.php
    • profile.php
    • login.php
    • other pages.php
  • page

    • contact.php

    • products.php

  • index.php

When someone comes to my website example.com it shows the index.php page. I use .htaccess to have clean URL's.

example.com/contact must include /page/contact.php into index.php.

example.com/user/username/profile must include /user/profile.php into user/index.php.

example.com/products/spinningbike include product.php into index.php and shows the details of product spinningbike.

This is my .htaccess file:

RewriteEngine On

RewriteRule ^login /user/login.php [L]
RewriteRule ^user/(\w+)/?$ /user/index.php?id=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?page=$1

I always get these php errors in my log files:

PHP Warning: include(): Failed opening 'page/user.php' for inclusion (include_path='.:/usr/local/lib/php') in /home/example.com/public_html/index.php on line 115, referer: http://www.example.com/user/wim/profile PHP Warning: include(page/user.php): failed to open stream: No such file or directory in /home/example.com/public_html/index.php on line 115, referer: http://www.example.com/user/wim/profile

I think the [L] is not working properly.

2
this is not related to apache and htaccess, this is a PHP error. on line 115 of index.php, your script tries to include the file page/user.php which does not exist. - ᴄʀᴏᴢᴇᴛ
Normally the .htaccess should stop at the second rewriterule, so my last rewrite rule should not be excecuted. That's why I get that php error. - wimdemo
No because the 2nd rule matches urls like http://example.com/user/username/ as you have "profile" at the end of the url, the last rule is used - ᴄʀᴏᴢᴇᴛ

2 Answers

0
votes

with the htaccess defined like this, the url example.com/user/username/profile will be matched with the last rule. because the 2nd rule will only match the urls of this format : example.com/user/[any word character]

word characters are a-z, A-Z, 0-9, _.

You have to change the 2nd rule to match this url

RewriteRule ^user/(\w+)/(\w+)/?$ /user/index.php?id=$1&page=$2 [L]

This way, the url example.com/user/username/profile will be rewritten example.com/user/index.php?id=username&page=profile

-1
votes

First, page/user.php does not exist in your current "folder system".

Then, even if it exists, it can maybe not work if you are using something like XAMPP, WAMP or MAMP because these softwares use a default root directory that is maybe not yours.

For exemple, in XAMPP this directory is C:\xampp\htdocs so, if your project is in a file (for exemple myProject) in this default directory, your URL to see the project render will be 127.0.0.1/myProject. Considering this, if you redirect to /page/user.php the redirect URL will be 127.0.0.1/page/user.php BUT you want to hit 127.0.0.1/myProject/page/user.php (C:\xampp\htdocs\page\user.php does not exist, so an exception is generated)

In XAMPP, to change the default root directory for the Apache webserver, you must edit the following lines:

DocumentRoot "C:/xampp/htdocs" <Directory "C:/xampp/htdocs">

to

DocumentRoot "C:/xampp/htdocs/myProject" <Directory "C:/xampp/htdocs/myProject">

in C:\xampp\apache\conf\httpd.conf