Please consider the content in my .htaccess
:
##
Options FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
## Allow a few SEO Files direct access.
RewriteRule ^robots.txt?$ robots.txt [L]
RewriteRule ^ads.txt?$ ads.txt [L]
RewriteRule ^sellers.json?$ sellers.json [L]
## Avoid rewriting rules for the admin section
RewriteRule ^(admin|resources)($|/) - [L]
## Set Ajax Request File
RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]
## Set controller with id
RewriteRule ^([^/] )/([0-9] )/?$ index.php?con=$1&id=$2 [L,QSA]
## Set controller with slug
RewriteRule ^([^/] )/([^/] )/?$ index.php?con=$1&slug=$2 [L,QSA]
## For paging
RewriteRule ^([^/] )/page/([0-9] )/?$ index.php?con=$1&page=$2 [L,QSA]
RewriteRule ^([^/] )/([^/] )/page/([0-9] )/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA]
## Set controller for only one parameter
RewriteRule ^page/([^/] )/?$ index.php?con=page&slug=$1 [L,QSA]
RewriteRule ^([^/] )/?$ index.php?con=$1 [L,QSA]
## Set home page
RewriteRule ^/?$ index.php?con=home [L]
Whenever I try to browse http://example.com/kahuk-ajax/?prefix=manage-story-vote
, this opened the index.php
instead of kahuk-ajax.php
!
What am I doing wrong?
CodePudding user response:
## Set Ajax Request File RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA] : ## Set controller for only one parameter : RewriteRule ^([^/] )/?$ index.php?con=$1 [L,QSA]
The request is first rewritten to kakuk-ajax.php
by the first rule, but the second to last rule then rewrites this to index.php
during the second pass by the rewrite engine.
You need to prevent that second rule from rewriting requests of the form kakuk-ajax.php
. If these URLs do not contain dots in the first path segment (that ordinarily delimits the file extension) then you could simply include a dot in the negated character class. For example:
## Set controller for only one parameter
:
RewriteRule ^([^/.] )/?$ index.php?con=$1 [L,QSA]
Alternatively, exclude specific file extensions:
RewriteCond %{REQUEST_URI} \.(txt|json|php)$
RewriteRule ^([^/] )/?$ index.php?con=$1 [L,QSA]
Or, any URL that looks like it has a file extension:
RewriteCond %{REQUEST_URI} \.\w{2,5}$
RewriteRule ^([^/] )/?$ index.php?con=$1 [L,QSA]
Aside:
RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]
This rule is a little contradictory. You are removing the query string with the trailing ?
, but then appending it again with the QSA
flag. If you wish to preserve the query string then you don't need either. And since you are capturing the URL-path, you might as well make use of this in the substitution string. For example:
RewriteRule ^(kahuk-ajax)/?$ $1.php [L]
Also, be wary of allowing an optional trailing slash here. This promotes duplicate content which could potentially cause SEO issues. Consider redirecting one to the other instead.
## Allow a few SEO Files direct access. RewriteRule ^robots.txt?$ robots.txt [L] RewriteRule ^ads.txt?$ ads.txt [L] RewriteRule ^sellers.json?$ sellers.json [L]
The ?
at the end of the regex makes the preceding character (t
and n
in the above examples) optional, which does not really make sense here. Also, you don't need to rewrite to the same file - you don't want any rewrite/substitution to occur here. So, the above is the same as:
RewriteRule ^(robots\.txt|ads\.txt|sellers.json)$ - [L]
The -
(hyphen) explicitly indicates "no substitution".
However, having included a "dot" in the (negated character class) directive above, this directive is redundant, except to "fail early".