Forum Moderators: phranque
I have a few questions about ReWrite rules as I am new to them.
I want a folder structure for my website, without actually putting the folder structure in if you catch my drift. I heard somewhere that ReWrite rules might be able to do this.
If someone types in "www.mydomain.com/folder1/folder2", would a ReWrite rule be able to change it to "www.mydomain.com/?v1=folder1&v2=folder2" as far as the PHP file is concerned, but not change the URL displayed in the browser?
And what implications does this have on search engine spiders and folder referencing?
If this doesn't make sense, let me know and I'll try and explain it better.
Thank you.
I have set up a folder on my server with the URL "/test/". Within this folder is a PHP called "index.php" and a HTACCESS file ".htaccess". The PHP file simple echos a word so I know it is working.
The HTACCESS is as follows,
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/test/([^/]+)/?$ /test/index.php?var=$1 [L]
I've taken this from the link jdMorgan posted
When I go to the URL "/test/", it works fine, but when I go to "/test/test2/", I just get a 404. I know my rewrite rules work on this server as I use them on another website we host, so I am not sure why my HTACCESS code isn't working.
Another thing to note is I use EasyPHP as a local webserver to test my websites. I know it has some limitations, but I get an internal server error when I try and test the .htaccess on my local server, but not on the live server.
So, any ideas?
N.B. There could be 5 or 6 folders used in my code, I am simply using 1 at the moment to get it working.
Options +FollowSymLinks
RewriteEngine on
RewriteRule /testtest/$ /index.php [L]
This did not load "/index.php" when "/testtest/" was entered. Does this mean mod_rewrite is loaded?
If so, how do I load it in the httpd.conf file?
Thank you!
After much Google'ing and playing, mod_rewrite is working on my server and I have achieved what I wanted.
BUT
My .htaccess file is a mess.
Here is the code, and I apologise in advance,
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)/$ /test/index.php?a=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ /test/index.php?a=$1&b=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /test/index.php?a=$1&b=$2&c=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /test/index.php?a=$1&b=$2&c=$3&d=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /test/index.php?a=$1&b=$2&c=$3&d=$4&e=$5 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /test/index.php?a=$1&b=$2&c=$3&d=$4&e=$5&f=$6 [L]
Now this does exactly what I want it to do, and the phrase "if it ain't broke, don't fix it" comes into mind, but surely there is a more efficient way of executing the above solution?
N.B. The above solution does not work if you type "/test/a/b", only if you type "/test/a/b/". How do I fix this aswell?
Thank you!
[edited by: Sekka at 8:31 pm (utc) on July 16, 2006]
You could simply rewrite *all* requests to /test/index.php and then have the index.php script 'decode' the parameters from the requested URL.
Your code doesn't look all that messy -- It is much worse if there is no fixed order of the request parameters and you have to allow for all variations of presense and order --- Now *that* can get messy.
> ...does not work if you type "/test/a/b", only if you type "/test/a/b/". How do I fix this as well?
End each pattern with ")/?$" where you now have ")/$", thus making the trailing slash optional.
Jim
I had to make some rules to exclude certain folders and if the address is to a specific file.
One more problem, I tried making a rule to make it only re-write if there is no "?" in the URL to no joy. Any ideas?
This is my code below,
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!/$
RewriteCond %{REQUEST_URI}!\.
RewriteRule (.*) http:/localhost/***/website_v3/$1/ [R=301,L]
RewriteCond %{SCRIPT_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)/$ /***/website_v3/index.php?a=$1 [L]
N.B. I the URL in rule 1 is wrong so the board won't parse it as a URL.
Technically, there is never a "?" *in* the URL. There may be one *appended* to the URL to denote a following query string, but "?" is not allowed within a URL. This is usually just being picky with terminology, but gets important when dealing with rewrites.
In order to check and manipulate queries appended to requested URLs, use RewriteCond.
Non-blank query:
RewriteCond %{QUERY_STRING} .
detect and extract product id:
RewriteCond %{QUERY)STRING} product_id=([^&]+)
and so forth. The back-reference created in the 2nd rewritecond would be available for use in the subsequent rewriterule by referencing "%1" instead of "$1".
Jim
[edited by: jdMorgan at 5:46 pm (utc) on July 17, 2006]