designer76 Posted July 20, 2012 Share Posted July 20, 2012 Hi, The code below works perfectly except for one thing. I want to be able to either add a trailing slash or make it so that the url can work with a trailing slash and without one. So: www.mysite.com/paintings and www.mysite.com/paintings/ would both work. As it stands right now, the trailing slash does not work no matter what I do. I have tried a few rewrite scripts that I have found, but the issue is that my url ends up not working, and it will read like: mysite.com/folder/mysite/html/paintings.php. The scripts that I have tried are basically reading the entire root folder on the server instead of just the HTML folder. My second question is is there a way that I can rewrite all of the urls on my site without having to create new code inside the .htaccess file? For example, in the code below, if I add an orange.php page inside of my acrylics folder, I am going to have to paste: RewriteRule ^artwork/([paintings]+)/([a-z]+)/([0-9]+) paintings/acrylics/orange.php?page=$3 [L] RewriteRule ^artwork/([paintings]+)/([a-z]+)/([a-z]+) paintings/acrylics/orange.php?page=viewall [L] into the .htaccess file, and so on, as I add new pages. That just seems like the wrong way to go about things, and the .htaccess file can get large fairly quickly if I have lots of pages, but this stuff is fairly new to me, so I could be wrong. Any help given is much appreciated. Thank you! RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.xml -f RewriteRule ^(.*)$ $1.xml RewriteRule ^designs/([0-9]+) designs.php?page=$1 [L] RewriteRule ^designs/([a-z]+) designs.php?page=viewall [L] RewriteRule ^paintings/([0-9]+) paintings.php?page=$1 [L] RewriteRule ^paintings/([a-z]+) paintings.php?page=viewall [L] RewriteRule ^artwork/([paintings]+)/([a-z]+)/([0-9]+) paintings/acrylics/blue.php?page=$3 [L] RewriteRule ^artwork/([paintings]+)/([a-z]+)/([a-z]+) paintings/acrylics/blue.php?page=viewall [L] RewriteRule ^artwork/([women]+)/([a-z]+)/([0-9]+) artwork/oils/red.php?page=$3 [L] RewriteRule ^artwork/([women]+)/([a-z]+)/([a-z]+) artwork/oils/red.php?page=viewall [L] Quote Link to comment https://forums.phpfreaks.com/topic/265999-i-need-some-help-and-have-a-few-questions-rewriting-urls/ Share on other sites More sharing options...
designer76 Posted July 21, 2012 Author Share Posted July 21, 2012 If this is any help, I found this code below that fixes most of my problems except for two issues. 1. I do not want the trailing slash to effect my search page at all, because it is causing problems. 2. I can not get it to work correctly with my paging. For example: both mysite.com/acrylics/viewall/ and mysite.com/acrylics/viewall (no trailing slash) worked with the mod_rewrite I posted above, but now it doesn't. I get an error saying that viewall.php does not exist. It seems like I may be close, but I just need some help to get this working properly. Can anyone assist? RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*[^/])/$ $1.php [L] # Forces a trailing slash to be added RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ RewriteRule (.*)$ /$1/ [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/265999-i-need-some-help-and-have-a-few-questions-rewriting-urls/#findComment-1363270 Share on other sites More sharing options...
designer76 Posted August 3, 2012 Author Share Posted August 3, 2012 Is anyone able to help with this? Quote Link to comment https://forums.phpfreaks.com/topic/265999-i-need-some-help-and-have-a-few-questions-rewriting-urls/#findComment-1366682 Share on other sites More sharing options...
DavidAM Posted August 4, 2012 Share Posted August 4, 2012 RewriteRule ^artwork/([paintings]+)/([a-z]+)/([0-9]+) paintings/acrylics/blue.php?page=$3 [L] First, I don't think this code is doing what you think it is doing. ([paintings]+) -- The parenthesis make it a capturing group so you can refer to the value as $1 in the rewrite. The square brackets make it a character class so any of the letters inside the bracket must be matched: "aginpst" are the letters in your character class (I just removed the repeating ones and alphabetized it). The "+" means 1 or more of the letters (from that class). So you are rewriting artwork/ants/bugs/2 to paintings/acrylics/blue.php?page=2. Second, what distinguishes this rewrite rule from the one you wanted to go to "organge"? They are exactly the same as far as I can see. Third, why are you capturing the first two values, you are not using them in the rewrite. I think you need to rethink what you are trying to accomplish. It might actually solve your question about orange. As for the trailing slash; I usually use something like: RewriteRule ^artwork/paintings/(blue)/([a-z]+)/([0-9]+)/? paintings/acrylics/$1.php?page=$3 [L] Which makes the trailing slash optional for that rewrite. Granted, I'm no expert at this, and I still have problems with the trailing slash from time-to-time. Quote Link to comment https://forums.phpfreaks.com/topic/265999-i-need-some-help-and-have-a-few-questions-rewriting-urls/#findComment-1366723 Share on other sites More sharing options...
designer76 Posted August 4, 2012 Author Share Posted August 4, 2012 RewriteRule ^artwork/([paintings]+)/([a-z]+)/([0-9]+) paintings/acrylics/blue.php?page=$3 [L] First, I don't think this code is doing what you think it is doing. ([paintings]+) -- The parenthesis make it a capturing group so you can refer to the value as $1 in the rewrite. The square brackets make it a character class so any of the letters inside the bracket must be matched: "aginpst" are the letters in your character class (I just removed the repeating ones and alphabetized it). The "+" means 1 or more of the letters (from that class). So you are rewriting artwork/ants/bugs/2 to paintings/acrylics/blue.php?page=2. Second, what distinguishes this rewrite rule from the one you wanted to go to "organge"? They are exactly the same as far as I can see. Third, why are you capturing the first two values, you are not using them in the rewrite. I think you need to rethink what you are trying to accomplish. It might actually solve your question about orange. As for the trailing slash; I usually use something like: RewriteRule ^artwork/paintings/(blue)/([a-z]+)/([0-9]+)/? paintings/acrylics/$1.php?page=$3 [L] Which makes the trailing slash optional for that rewrite. Granted, I'm no expert at this, and I still have problems with the trailing slash from time-to-time. Thanks for the reply sir, but I am still confused. I am basically new to url rewriting, so I don't really know much about what you stated. I wasn't trying to get get over-complicated with the code that I posted, it's just what I was able to piece together that worked with my website paging code. I am just looking for as simple of a rewrite as possible (that can work with my paging) that will prevent me from having to create a new rewrite rule every time I add new folders/pages/content to my site. Quote Link to comment https://forums.phpfreaks.com/topic/265999-i-need-some-help-and-have-a-few-questions-rewriting-urls/#findComment-1366849 Share on other sites More sharing options...
designer76 Posted August 5, 2012 Author Share Posted August 5, 2012 Actually, I have figured out the issue with having to make a new rewrite rule every time I add a new page or folder with new content. The only issue I am having now is making this condition: RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/$ /$1 [R=301,L] Work with my paging condition here: RewriteRule ^designs/([0-9]+) designs.php?page=$1 [L] RewriteRule ^designs/([a-z]+) designs.php?page=viewall [L] RewriteRule ^artwork/([paintings]+)/([a-z]+)/([0-9]+) paintings/acrylics/blue.php?page=$3 [L] RewriteRule ^artwork/([paintings]+)/([a-z]+)/([a-z]+) paintings/acrylics/blue.php?page=viewall [L] The condition on the top allows people to type a trailing slash at the end of the url and have it redirect back to a url that does NOT have the trailing slash on it. The condition I was using before would throw an internal server error any time someone typed a trailing slash at the end of the url. The problem is that now my paging is throwing an internal server error with the new trailing slash condition that I am using. I have tried a bunch of things, but I can't figure out how to make the two work together. If anyone could help me with this last little bit, that would be awesome. I am almost there. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/265999-i-need-some-help-and-have-a-few-questions-rewriting-urls/#findComment-1366954 Share on other sites More sharing options...
designer76 Posted August 5, 2012 Author Share Posted August 5, 2012 Actually, I was able to solve it. I found and manipulated some code that worked for me. Quote Link to comment https://forums.phpfreaks.com/topic/265999-i-need-some-help-and-have-a-few-questions-rewriting-urls/#findComment-1366974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.