gordon.c Posted December 10, 2009 Share Posted December 10, 2009 Hi I am using this code in .htaccess but cant find a correct solution that would automatically put slash at the end or url if missing. Can you please help me with it... Options +FollowSymLinks RewriteEngine on RewriteRule ^([A-Za-z0-9-]+)$ profile/$1/ [R] RewriteRule (.*)/$ view.php?profile=$1 I cant work out why does not this code work properly - ending the url with slash Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/ Share on other sites More sharing options...
cags Posted December 10, 2009 Share Posted December 10, 2009 Well the exact rewrite would depend on your actual data structure. An example... RewriteRule ^([A-Za-z0-9-]+)$ /profile/$1/ [R] RewriteRule ^profile/(.*)/$ /view.php?profile=$1 Will change the users address bar from http://domain.com/bob to http://domain.com/profile/bob/, the user will actually see http://domain.com/view.php?profile=$1. If that's not what your after (which I don't think it is) you will have to be more specific giving an example url you wish to start with, what it should be changed to and what they should actually see. Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-974794 Share on other sites More sharing options...
mrMarcus Posted December 10, 2009 Share Posted December 10, 2009 two things to note here: 1. this code will append a forward slash to the end of a URL if one is not present: RewriteCond %{HTTP_HOST} !^www.your-site.com$ [NC] RewriteRule ^(.*)$ http://www.your-site.com/$1 [R=301,L] 2. this one gives allows for either by having a ? immediately following the /: RewriteRule ^([A-Za-z0-9-]+)/?$ view.php?profile=$1 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-974924 Share on other sites More sharing options...
gordon.c Posted December 11, 2009 Author Share Posted December 11, 2009 I dont get why doesnt this code do the slash at the end thing RewriteRule ^([A-Za-z0-9-]+)$ profile/$1/ [R] from what I learnt it should be enough Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975465 Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 What URL are you using to test it? Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975473 Share on other sites More sharing options...
gordon.c Posted December 11, 2009 Author Share Posted December 11, 2009 My initial url is http://127.0.0.1/mywebsite/profile/view.php?profile=name My output url is http://127.0.0.1/mywebsite/profile/name/ for this the code I use in my .htaccess does not work in terms of adding the slash at the end Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975505 Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 That URL doesn't match your RewriteRule. Your pattern matches something that only contains a-z, 0-9 or a dash. I'd guess that you've done nothing to set your site up in Apache thus making your DocumentRoot http://127.0.0.1/ meaning that the pattern you need to match is mywebsite/profile/name which contains forward slashes. Your pattern to add a trailing slash would probably need to be something more like... RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ $1/ [R] Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975522 Share on other sites More sharing options...
gordon.c Posted December 11, 2009 Author Share Posted December 11, 2009 That URL doesn't match your RewriteRule. Your pattern matches something that only contains a-z, 0-9 or a dash. I'd guess that you've done nothing to set your site up in Apache thus making your DocumentRoot http://127.0.0.1/ meaning that the pattern you need to match is mywebsite/profile/name which contains forward slashes. Your pattern to add a trailing slash would probably need to be something more like... RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ $1/ [R] I am sorry I tried according to this and it still does not work Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975535 Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 Missed a slash... RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ /$1/ [R] ...should work, I tested it on my end. Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975540 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 Missed a slash... RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ /$1/ [R] ...should work, I tested it on my end. What is wrong with my server, It didnt work again... I tried several different changes but still no success. Is the second line all right together with the first one? Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975738 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 We were discussing getting the slash on the end, does that line or does that line not append the slash to the end of the url, forget the second rule for now. Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975911 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 You are right... Unfortunately the first rule does not add the slash Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975926 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 So just to confirm, you are saying that if you have a .htaccess file with just the following in it... Options +FollowSymLinks RewriteEngine on RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ /$1/ [R] The slash is not added in the address bar. Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975934 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 yes correct Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975943 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 Have you modified your Apache set-up in any way to add virtual hosts or anything? Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975945 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 Not that I am aware of... The only time when the rewrite rule added the slash was when I was using instead of name a number http://127.0.0.1/mywebsite/profile/name/ http://127.0.0.1/mywebsite/profile/0123456/ With url ending with a number it added the slash however it never worked for me with url ending with string Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975953 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 What URL are you using to test your rewrite? Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975955 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 http://127.0.0.1/mywebsite/profile/name.surname With slash this url shows up and there is no problem however without the slash, apache return server error "Not found" Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-975992 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 *sigh* that URL doesn't match your pattern, it has a fullstop in it. Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-976006 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 Oh... good point, but still if I remove the fullstop from the url the browser says page not found (not the server this time the browser) I dont get it really, the mod rewrite definition does not specify fullstop character but as long as its ended with slash its fine - how come? Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-976039 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 Page not found is exactly the error you want, providing it has added the slash, which is all we we're working on. Once that is working correctly we can work on the second redirect to serve up a different page to what the address says. Your second rule will want to be something along the lines of... RewriteRule ^mywebsite/profile/([A-Za-z0-9-]+)/$ /mywebsite/profile/view.php?profile=$1/ Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-976051 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 I dont think there is a problem with the second line any more... I am curious about the first one still, cant I put somehitng into the range somehow so that it is ready also for fullstops because I really need it there Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-976119 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 Yer, in theory you can just add a fullstop to the character class, but I'm not sure if it is considered a valid URL character. Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-976124 Share on other sites More sharing options...
gordon.c Posted December 12, 2009 Author Share Posted December 12, 2009 I guess it didnt... How about instead of struggling how to add that bloody slash, what if I made the rewrite rule not to show the ending slash at all, like twitter it has for example Quote Link to comment https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/#findComment-976176 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.