Jump to content

mod_rewrite in need of guidelines


ckrudelux

Recommended Posts

So I'm trying to find the key to unlock the knowledge of htaccess files and mod_rewrite.This is what I want to do and what I understand mod_rewrite is how to do it.Say this is my address now:www.example.com/?page=41&epara=12,45www.example.com/index.php?page=41&epara=12,45I want it to look like this:www.example.com/value_of_page/?epara=12,45www.example.com/value_of_page/index.php?epara=12,45So can I use dynamic values to my address and how do I set them in the htaccess file and is it possible to do what I want, some guidelines would be appreciated :)

Link to comment
Share on other sites

  • 2 weeks later...

You need to write different conditions for each of those. For the first, you would want to add a line something like the following to your .htaccess file:RewriteRule www\.example\.com/(.*)/epara=(.*)$ / www\.example\.com/?page=$1&epara=$2For that to work, the URL should not have the '?' in it, i.e.www.example.com/41/epara=12,45would yieldwww.example.com/?page=41&epara=12,45You can get rid of the epara=12,45 part as well, if you want to make it even neater:RewriteRule www\.example\.com/(.*)/(.*)$ / www\.example\.com/?page=$1&epara=$2I personally hide my index.php page like this:RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html|php)\ HTTPRewriteRule ^(.*)index\.(php5|html|php)$ /$1 [R=301,L]Without doing this, the above rule won't really work, because a domain without a document name resolves to index.* anyway. Note that most don't need the php5 option but my webhost needs it to resolve php5 pages...Oh, and I also tend to get rid of '?' by letting a trailing forward slash always equate to a question mark:RewriteRule ^(.*)\.(php|php5|html)/(.*)$ /$1\.html?$2

Link to comment
Share on other sites

You need to write different conditions for each of those. For the first, you would want to add a line something like the following to your .htaccess file:RewriteRule www\.example\.com/(.*)/epara=(.*)$ / www\.example\.com/?page=$1&epara=$2For that to work, the URL should not have the '?' in it, i.e.www.example.com/41/epara=12,45would yieldwww.example.com/?page=41&epara=12,45You can get rid of the epara=12,45 part as well, if you want to make it even neater:RewriteRule www\.example\.com/(.*)/(.*)$ / www\.example\.com/?page=$1&epara=$2I personally hide my index.php page like this:RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html|php)\ HTTPRewriteRule ^(.*)index\.(php5|html|php)$ /$1 [R=301,L]Without doing this, the above rule won't really work, because a domain without a document name resolves to index.* anyway. Note that most don't need the php5 option but my webhost needs it to resolve php5 pages...Oh, and I also tend to get rid of '?' by letting a trailing forward slash always equate to a question mark:RewriteRule ^(.*)\.(php|php5|html)/(.*)$ /$1\.html?$2
Sounds really good but I have no clue on what you just wrote mean at all, I have never coded a .htaccess file before so don't really know what any of your tags do or mean. Sure I can try to tweak what you have written but I haven't a clue on what I'm doing and I want to learn.
Link to comment
Share on other sites

Sounds really good but I have no clue on what you just wrote mean at all, I have never coded a .htaccess file before so don't really know what any of your tags do or mean. Sure I can try to tweak what you have written but I haven't a clue on what I'm doing and I want to learn.
Yeah, if only there was some sort of worldwide network of computers where you could search for information on any topic you had a question about.
Link to comment
Share on other sites

Yeah, if only there was some sort of worldwide network of computers where you could search for information on any topic you had a question about.
Ha! Burrrrrn.You need to create a file called .htaccess (it has no filename, only the extension, which can make it hard to find depending on your file view) and upload it into the root directory of your fileserver. All it needs to contain is:
RewriteEngine OnRewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html|php)\ HTTPRewriteRule ^(.*)index\.(php5|html|php)$ /$1 [R=301,L]#Translate '/' into '?' after file extension, to tidy up parametersRewriteRule ^(.*)\.(php|php5|html)/(.*)$ /$1\.html?$2#Translate www.example.com/1/2 www.example.com/page=1¶=2RewriteRule www\.example\.com/(.*)/(.*)$ / www\.example\.com/?page=$1&epara=$2

Link to comment
Share on other sites

Yeah, if only there was some sort of worldwide network of computers where you could search for information on any topic you had a question about.
Yeah, Google is a largely growing network which is highly intelligent, too bad I haven't found anything I understood where :) W3SCHOOLS should have a tutorial for .htaccess files :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...