anavaras lamurep
anavaras lamurep

Reputation: 1513

.htaccess rewrite rule is not working

I have .htaccess file in same folder where my code resides.

# Turn rewrite engine on
RewriteEngine on

RewriteBase /

RewriteRule ^/mobile/list/$ RestController.php?view=all [NC,L]
RewriteRule ^/mobile/list/([0-9]+)/$ RestController.php?view=single&id=$1 [NC,L]

Rewritebase added as per hosting provider (https://www.hostinger.in/knowledge-base/489) But my rewrites are not working if i try to load the url

jobstatuscheck.esy.es/mobile/list/

It is not redirecting to jobstatuscheck.esy.es/RestController.php?view=all

Upvotes: 0

Views: 1077

Answers (1)

arkascha
arkascha

Reputation: 42974

A RewriteRule works on a relative path, not on an absolute path, when used inside a dynamic configuration file. That is because those files are interpreted relative to their location in the document hierarchy. This is clearly pointed out in the documentation of Apache's rewriting module.

That means you need to either use relative paths in your patterns or a somewhat more flexible approach (note the additional question marks...):

RewriteEngine on
RewriteBase /
RewriteRule ^/?mobile/list/?$ RestController.php?view=all [NC,L]
RewriteRule ^/?mobile/list/([0-9]+)/?$ RestController.php?view=single&id=$1 [NC,L]

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Upvotes: 1

Related Questions