help using mod_rewrite on both dynamic and static URL's
I looked at this topic: configuring search engine friendly url (mod_rewrite) and understand how the rewrite rules would apply, but need help understanding how to adopt them for websites that have static and dynamic URL's.
From the topic above, the rewrite rules would first look for a file that existed in the URL, and deliver it. So that would cover my static pages.
Question 1: would indexing be preserved (in other words, domain.com/foo would show domain.com/foo/index.php)?
Question 2: is there a way to use a single template file with these static pages (so the static file is really only the content)? At the moment, my rewrite rules point all URL's to a main template file that looks for the requested file and then includes it.
Moving on, the rewrite rules would redirect you to index.php for any URL's that didn't match the above rule. I assume that's where I'd want my code to determine if it's an actual URL for my site, and deliver the content accordingly.
Question 3: redirecting to index.php isn't going to pass along any variables to process. If I used index.php?url=$1 would that account for both domain.com/hi and domain.com/hi/there?
Question 4: what steps can I take to ensure the URL's processed are legitimate (and not xss attacks or anything like that)? Should I set up a separate table in my MySQL database that stores all the possible URL's along with the ID of the content? I assume that's how Wordpress works. Or, if not using MySQL, would I开发者_运维知识库 do this with a class, or array?
I know it's a lot, thanks in advance for everyone's help!
Just for the record: If not mentioned otherwise, all following answers refer to these rules that were mentioned in jiewmeng’s configuring search engine friendly url (mod_rewrite):
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Question 1: would indexing be preserved (in other words, domain.com/foo would show domain.com/foo/index.php)?
If you refer to index documents specified by DirectoryIndex
, then, yes, as REQUEST_FILENAME would refer to an existing directory, the comparator -f
would yield true and no rewrite would take place.
Question 2: is there a way to use a single template file with these static pages (so the static file is really only the content)? At the moment, my rewrite rules point all URL's to a main template file that looks for the requested file and then includes it.
No, that is not possible. Static files that are not further processed by a handler are directly sent to the client.
Question 3: redirecting to index.php isn't going to pass along any variables to process. If I used index.php?url=$1 would that account for both domain.com/hi and domain.com/hi/there?
In the mentioned example you can use $_SERVER['REQUEST_URI']
to get the original requested URI path and query string as it appeared in the request line. So even the original requested query string will be preserved.
Question 4: what steps can I take to ensure the URL's processed are legitimate (and not xss attacks or anything like that)? Should I set up a separate table in my MySQL database that stores all the possible URL's along with the ID of the content? I assume that's how Wordpress works. Or, if not using MySQL, would I do this with a class, or array?
Yes, you should definitely know what URLs are valid and what are invalid. And in general it’s the application that does this decision by parsing the request and trying to find the requested resource. How that is done depends on what your application does and how it does that.
精彩评论