regex - Mod Rewrite load index.php on bad path

353

These are the rewrite rules I'm using. At this point I'm trying to force bad urls to load index.php. The html tag<base href="/directory-name/" /> is in the top of index.php, so my css, images and other assets are loading properly.

Options -Indexes +FollowSymLinks
RewriteEngine On 

RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/(.*) /index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^(.*)/(.*) /index.php?x=$1&y=$2 [L]
RewriteRule ^(.*) /index.php?x=$1 [L]

Url Examples:

www.domain.com/directory-name/
www.domain.com/directory-name/index.php
www.domain.com/directory-name/index.php/arg1/arg2/arg3

www.domain.com/directory-name/anything-here
www.domain.com/directory-name/anything-here/arg1/arg2/arg3

The first 3 url examples work fine and loadindex.php. But the 4th and 5th urls fail and dumps me back to the html root / where a xampp index exists. I'm assuming the failure of the last url rewrite is because the index.php is in a sub-directory off of root?

How would I fix that? How would I force the urls to loadwww.domain.com/directory-name/index.php even if something else is in the path?

823

Answer

Solution:

Try these rules:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2 [L]
RewriteRule ^([^/]+)/?$ index.php?x=$1 [L]

People are also looking for solutions to the problem: php - Symfony 2.3 - Disable bundles based on service data

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.