1

My basic requirement is:

  1. Remove .php extensions from all the urls.
  2. Rewrite the urls from http://localhost/unsync/softwares/page_name/sub_category/ to http://localhost/unsync/softwares.php?p=page_name&sub_cat=sub_category

The following is my .htaccess code:

# Do not remove this line, otherwise mod_rewrite rules will stop working
Options +MultiViews
RewriteEngine On
RewriteBase /

#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

#Prevent directory listings
Options All -Indexes

#Error Documents
ErrorDocument 400 /unsync/error.php?code=400
ErrorDocument 401 /unsync/error.php?code=401
ErrorDocument 402 /unsync/error.php?code=402
ErrorDocument 403 /unsync/error.php?code=403
ErrorDocument 404 /unsync/error.php?code=404
ErrorDocument 500 /unsync/error.php?code=500
ErrorDocument 503 /unsync/error.php?code=503

#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]
RewriteRule softwares/(.*)/(.*)/$ /softwares.php?p=$1&sub_cat=$2 [L]

DirectoryIndex index.php

The problem I am facing is that, RewriteRule fails. I mean, when I try to access softwares/page_name/sub_category, its throwing a 404 error.

Note: Its removing the .php extensions properly and working fine with normal pages.

Manikiran
  • 2,618
  • 1
  • 23
  • 39

3 Answers3

1

The problem is that your rewrite rule rewrites all requests to /unsync/request.php, you have to check the existance of php file before rewriting the request,

    #Remove extensions
    RewriteCond %{DOCUMENT_ROOT}/unsync/$1.php -f
    RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]

Or you can simply exclude the slash in pattern so that it can not conflict with your other rule

    RewriteRule ^([^\/.]+)$ /unsync/$1.php [NC,L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
0

Rewrite rules are tried in order.

This means softwares/page_name/sub_category is rewritten by the first rule to /unsync/softwares/page_name/sub_category.php, which is not found. Therefore, you get a 404 Not found error.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

After a long day of research, I could finally resolve my issue on my own as follow (if anyone facing similar issue is searching for solution):

#If both p and sub_cat are issued
RewriteRule ^softwares/(.+)/(.*)$ /unsync/softwares.php?p=$1&sub_cat=$2 [NC,L]

#If only p is issued
RewriteRule ^softwares/(.*)$ /unsync/softwares.php?p=$1 [NC,L]

#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /unsync/$1.php [NC,L]

DirectoryIndex index.php
Manikiran
  • 2,618
  • 1
  • 23
  • 39