0

I have been searching all over the web for a solid 2 hours now trying different things, and none of what i found would work.

I want to redirect to a non-existent folder and then convert that into a query-string - for example an anchor tag that leads to "domain.com/test/" But .htaccess should intercept that and change it into "domain.com/index.php?p=test" Behind the curtains without the viewer seeing the query-string

I am not sure what I am doing wrong, but I have tried pretty much everything on google - page 1 and two at least :P

Any help is greatly appreciated.

1

1 Answer 1

1

mod_rewrite has attribute tests that allows you to test if the requested url is a file or a directory. See the documentation.

To use this, you use a RewriteCond to test %{REQUEST_FILENAME}. You can then redirect or internally rewrite the url. A redirect will change the url visibly for the client, while an internal redirect will only show a different page without actually changing the url.

#The requested url is NOT a directory AND NOT a file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [L]

If you want an external redirect to happen, add the R-flag. The R=301 flag will make the redirect permanent, but only make it permanent if all rules work as you expect them to work.

Sign up to request clarification or add additional context in comments.

7 Comments

Okay i have tried this and i can go to /Test/p/Home and alike but all the relative links are broken aswell as the sql functions - So I am not sure I am liking this correctly... PS. /Test is the folder in my domain root Want to display this "localhost:8888/Test/Home" in the URL but php should interpret it as localhost:8888/Test/?p=Home
Try to use RewriteBase /Test/. mod_rewrite doesn't change links on the page, so if relative links are broken, you must have changed something else.
Okay Well it works I just had to not use /p/Home as I was trying... Is there a way to detect what it uses? so eg. ?p=Home will go to /Home And /refresh=213124123 will go to /refresh/213124123? or something like that - basically detect which querystring is called :)
Use RewriteCond %{QUERY_STRING} ^p=(.*)$ with %1 as replacement and RewriteCond %{QUERY_STRING} !^p= and RewriteCond %{QUERY_STRING} ^([^=]+)=(.*)$ with %1 and %2 as replacements. Read the documentation and search for examples on how to use them.
I am not sure how that works - apache and regex is the things i am having a very hard time learning sadly :/ would you be able to prove the entire code? because when i look for examples, I always manage to screw them up somehow :P
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.