Mohammad
Mohammad

Reputation: 7418

Caching with Apache Mod Redirect

So I want to keep all my cached pages in a folder called /cache

and I was thinking to have the generic url

example.com/not a real folder

silently redirect to

example.com/cache/not-a-real-folder.html

and if the above file does not exist have it redirect to

index.php?page=not a real folder 

but how do I design the Apache Mod Rewrite code that will do all this, is it even possible?

I'm new to these things so any help would be greatly appreciated! Thank you so much!

Upvotes: 0

Views: 363

Answers (1)

clmarquart
clmarquart

Reputation: 4711

# First check if the file requested exists in the cache folder,
# if it does, rewrite the url to the cached version
# -The %{REQUEST_URI} will probably start with a '/'
RewriteCond /cache%{REQUEST_URI} -f
RewriteRule (.*) /cache$1.html [L,NC]

# If the request makes it here, the file requested is not in the cache,
# so we can rewrite the request to the index.php page for processing
RewriteRule (.*) /index.php?page=$1 [L]

For clairification, are your requests coming in with spaces, and you need them rewritten with dashes? Because that adds some more complexity...

Hope this helps.

Upvotes: 1

Related Questions