Is it possible to allow only the correct pretty url?

96 views Asked by At

I have these pretty urls:

  • app/category/overview
  • app/category/new
  • app/category/edit/4

  • app/product/overview

  • app/product/new
  • app/product/edit/4

.htaccess

RewriteEngine On
RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.phpcontroller=$1&action=$2&id=$3 [NC,L]

Is it possible to define that any other other url goes back to the index?

p.e. app/i/am/the/fastest/man/alive ---> app/

Help would be much appreciated!

2

There are 2 answers

0
anubhava On BEST ANSWER

You can have a new rule for that:

RewriteEngine On
RewriteBase /app/

RewriteRule ^([a-z]+)/([a-z]+)/?([0-9]*)$ index.php?controller=$1&action=$2&id=$3 [NC,L,QSA]

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

I have added ? after index.php

0
Panama Jack On

The answer to your question

Is it possible to allow only the correct pretty url?

Sorta, but you have to be more specific on your rules to limit them to that. Take this example.

RewriteEngine On
RewriteBase /app

#rewrite rule if starts with /app/product or /app/category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(product|category)/([a-z]+)/([0-9]*)/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
#else redirect to homepage
RewriteRule . app/ [R=301,L]

This will ensure that only URL that are like http://example.com/app/product/1 will be rewritten and all others like http://example.com/app/some/other/page to your home page.