Forum Moderators: phranque
I have two conditions which I want to happen
1) regardless of the url, I want everything to be pointed to a file I specify such as index.php.
the code I have does what I need.
RewriteEngine on
RewriteRule ^(.*)$ index.php [L]
2) I want to put an trailing slash on a folder request in the event users forget to put it in
the code I have for this also works fine for me.
RewriteEngine on
rewritebase /
RewriteRule ^folder_name$ folder_name/ [R=301]
But when I put the rules together neither works.
My goal is to have anyone referencing this folder o check for the trailing slash and input it if it doesn't exist in the url and then for all url values regardless to be pointed to a index.php file. For ex. a url path of article/title/content/content.html would be pointed to a index.php file which does the parsing of the values.
Like I said the rules work separately but not when I put them together.
RewriteEngine on
rewritebase /
RewriteRule ^folder_name$ folder_name/ [R=301]
RewriteRule ^(.*)$ index.php [L]
Can anyone explain to me why they dont work together? and what I need to do to get them to work?
Add a RewriteCond to prevent the loop:
RewriteEngine on
RewriteBase /
#
# Add missing trailing slash to folder_name and redirect
RewriteRule ^folder_name$ http://www.example.com/folder_name/ [R=301,L]
#
# Rewrite all requests except for index.php to index.php
RewriteCond $1 !index\.php$
RewriteRule (.*) /index.php [L]
Note that as you specified, all requests are sent to the script, including requests for /folder_name/, robots.txt, custom error pages, all images, CSS stylesheets, etc.
Jim
Like I said they work as intended separately but when I put them together, neither objectives are achieved.
Neither does it put a trailing slash but it also doesnt refer all urls to the index.php file as I intended.
Now I have been playing with the rules a bit and I think mostly the problem is due to the last rule being to broad and its messing up the trailing slash rule as well.
So I have been toying with it to narrow the second rule, and this seems to work. Better anyways. Its more specific about what it is looking for so it does not interfere with the trailing slash routine.
RewriteEngine on
RewriteRule ^([a-zA-Z0-9/]+)/([a-zA-Z0-9]+) /index.php?id=$2 [L]
RewriteRule ^([a-zA-Z0-9]+)/?$ /index.php?id=$1 [L]
I'm like 99% where I want to be.
The only problem here is if there is a non-alpha character in the url like "&" or "-" or "_" it doesnt know what to do.
For ex: if the url looks like [domain.com...]
its giving me a 404 not found error.
With my limited understanding the reason for this is because the conditions I am checking for only take into consideration for alpha or numeric values in the url.
What can I add to the rule to look for non-alpha characters without using the broader ".*" which is whats was causing the problem to begin with?
That fixed it, thanks alot.
Boy this whole thing is new to me but its also pretty fun.
Right now I feel like a sponge just waiting to soak up this stuff.
I'll check out the tutorial like you asked. Also if you have any other websites that you know of that shows practical examples I'd appreciate any pointers.
Thanks again.
Sorry to keep bothering you with this but that last piece brings me to 99.9% now.
The mod rewrite rule now works the way it should most of the time.
However, theres one little anomoly.
when I have a url like [domain.com...]
the value it returns is "first_" not "first_&_second"
if the url is first_second it works fine.
it seems only when the url has the "&" in it, it returns only the first part of the value and not the whole.
I'm using he rewrite rule as you specified in your last post.
Any thoughts?
My suspicion is that "&" is actually used in the url for query in like query=123&id=2
If so is there a work around or maybe I need to parse the values separately?