2

I am developing a PHP web application. I have pages, posts, categories, product and etc.

Here is my .htaccess file to create SEO friendly URL:

Options -Indexes
RewriteEngine on
RewriteRule ^Page/(.*)/(.*)$ Page.php?Page_Id=$1&Page_Title=$2 [NC,L]
RewriteRule ^Product/(.*)/(.*)$ Product.php?Product_Id=$1&Product_Title=$2 [NC,L]
RewriteRule ^Category/(.*)/(.*)$ Category.php?Category_Id=$1&Category_Title=$2 [NC,L]
RewriteRule ^Single/(.*)/(.*)$ Single.php?Post_Id=$1&Post_Title=$2 [NC,L]
RewriteRule ^Blog/(.*)/(.*)$ Blog.php?Category_Id=$1&Category_Title=$2 [NC,L]

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


<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>

When I visit Page.php?Page_Id=13 it works. And also when I visit Page/13/Page-Title it works. Everything is good when I test my application on the server, but it doesn't work in wamp localhost.

I active

LoadModule rewrite_module modules/mod_rewrite.so

in Apache http.conf and here is my vhost:

    <VirtualHost *:8080>
    ServerName localhost
    DocumentRoot c:/wamp64/www
    <Directory  "c:/wamp64/www/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>


<VirtualHost *:8080>
    ServerName schoolarshop
    DocumentRoot "c:/wamp64/www/schoolarshop"
    <Directory  "c:/wamp64/www/schoolarshop/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride none
        Require local
    </Directory>
</VirtualHost>

Example of error in localhost is:

Notice: Undefined index: Page_Id in Page.php on line 8

Why does it work on the server and not in localhost?

I Googled a lot but it can't find anything.

2
  • Using WordPress? you need to update the site URL and permalinks. – Simon Hayter Jul 3 '17 at 19:55
  • no i use pure php – Mohammadreza Jul 4 '17 at 3:37
4
Options +Indexes +Includes +FollowSymLinks +MultiViews

Why are you explicitly enabling MultiViews in your server config? This is likely the cause of your problem. Try removing +MultiViews from your server config (above), and/or disable MultiViews at the top of your .htaccess:

 Options -Indexes -MultiViews

MutliViews is disabled on a default Apache install, however, some (shared) hosts do seem to enable it. So, disabling it in .htaccess as a matter of course is probably recommended.

(But also, why are you enabling Includes - is this really necessary? Since you are using PHP, this is unlikely.)

The effect of MultiViews is noticeable because you have effectively removed the file extension to create your user-friendly URLs.

With MultiViews enabled (part of mod_negotiation)... when you request Page/13/Page-Title, Apache tries to find a matching file in the current/root directory that matches the basename Page, by trying various extensions that would return the appropriate mime-type. This occurs before mod_rewrite is able to rewrite your friendly URL. So, MultiViews ends up rewriting your request to Page.php without passing any of the URL params (the rest of the URL will likely be seen as PATH_INFO), hence your "Undefined index" PHP notice.

(Although you should also be checking for the existence of this $_GET variable in your PHP script, which you don't seem to be doing.)

5
  • thanks.it work but now i have anither problem.when i visit Page/13/page-title if the lenght of page title is less than 5 it work but for more than 5 character it shows The requested URL Page/13/blah-blah-blah-blah was not found on this server. – Mohammadreza Jul 4 '17 at 16:25
  • There doesn't appear to be anything in your .htaccess file that would be dependent on the page title length. (Everything after the second slash is simply passed through to the Page_Title URL param.) Examine the URL parameters that Page.php is receiving - are these as expected? However, I would not expect the page title to control which page is returned either - I would expect the Page_Id to control this? – MrWhite Jul 4 '17 at 17:35
  • http://localhost:8080/schoolarshop/Page/13/قوانین-و-مقررات the top url shows error but http://localhost:8080/schoolarshop/Page/13/قوانینworks – Mohammadreza Jul 4 '17 at 18:42
  • Maybe a character encoding or application logic problem in your server-side PHP/DB code? As mentioned, there's nothing in the code you have posted (Apache directives) that would explain this. What version of Apache are you using? – MrWhite Jul 6 '17 at 0:31
  • 1
    sorry i do it in a wrong way.i change RewriteRule ^Page/(.*)/(.*)$ to RewriteRule ^Page/([\s\S]*)/([\s\S]*)$ and it worked.thank you for your answers – Mohammadreza Jul 6 '17 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.