1

Im trying to rewrite a request from an URL like:

www.domain.com/xyz

to

www.domain.com/index.php?goto=xyz

The plan is to handle requests myself then in index.php. It would be nice if the URL would stay www.domain.com/xyz, and it would be also nice to not redirect when there really is a folder called xyz.

Ive tried this:

RewriteRule ^/(.*)$ /index.php?goto=$1 [R=301,NC,L]

But this does not keep the URL, doesn't check if there is already the folder xyz and most of all it always goes to index.php?goto=index.php

Andy help would be greatly appreciated!

1 Answer 1

2

You need to check for existence of files and directories in your rule. Keep this rule in root .htaccess:

RewriteEngine On

RewriteRule ^index\.php$ - [L,NC]

# 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?goto=$1 [QSA,L]
4
  • Thanks a lot, works great so far! Theres one little problem left. If i request an URL like /test and test is a real folder, it redirects me to index.php?goto=index.php instead of going to that specific folder. Jun 26, 2015 at 20:39
  • 1
    That won't happen with these rules. Clear your browser cache and retry.
    – anubhava
    Jun 26, 2015 at 20:41
  • Soon after I did this, i got a PHP fatal error: allowed memory size of 262144 bytes exhausted. Had this rewrite caused a side effect? Jun 27, 2015 at 0:53
  • 1
    No rewrite rule is not related to PHP error. Look into your php.ini to allocate more memory or there may be some PHP code issue.
    – anubhava
    Jun 27, 2015 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.