Rommeo Posted July 30, 2008 Share Posted July 30, 2008 hi i m new about programming. And i m building my own web-site. i just wanna use hotlinks like ; instead of this : www.mysite.com/index.php?page=contact i wanna use this : www.mysite.com/myname/contact.html ( my name here is just for search engines. I wont use it/dunno how to add .html at the end yet ) and my .htaccess file is here ; RewriteEngine On RewriteBase / RewriteRule ^myname/([^/\.]+)/?$ index.php?page=$1 [L] ErrorDocument 401 http://www.mysite.com ErrorDocument 403 http://www.mysite.com ErrorDocument 404 http://www.mysite.com ErrorDocument 500 http://www.mysite.com This code is not working. When i enter "www.mysite.com/myname/contact" ( could not find how to add .html in the end by the way ), i see that my website is without css / images actually nothing. But it gives me to correct contact information as only texts. Can anyone please help me about this ? Thanx in advance. Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/ Share on other sites More sharing options...
obsidian Posted July 30, 2008 Share Posted July 30, 2008 Keep in mind that client scripts (JS, images, CSS) use relative paths from the location of your current address, so it is going to try and include the images and such relative from http://www.mysite.com/myname/contact, which is actually nonexistent. To get around this, you can either use absolute paths, or (my personal choice) use a base href to tell the browser where to look. So, let's say that your images, JS and CSS are all off your root (http://www.mysite.com). You would set up a base href to point to that location, then you could include them relatively from there. <!-- Inside the HEAD tags --> <base href="http://www.mysite.com" /> <!-- Inside your body --> <img src="images/img01.jpg" /> <!-- actually hitting http://www.mysite.com/images/img01.jpg no matter where you are --> As for adding the HTML to the front of your URL, try this (also, this will allow it to work with or without the .html: RewriteRule ^myname/([a-zA-Z]+)(\.html)?/?$ index.php?page=$1 [L] Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-603670 Share on other sites More sharing options...
Rommeo Posted July 30, 2008 Author Share Posted July 30, 2008 Thank you so much obsidian. But there is something wrong i think, i changed the rule as you said. Now it goes on the main page ( 404 error i guess ) Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-603681 Share on other sites More sharing options...
Rommeo Posted July 30, 2008 Author Share Posted July 30, 2008 Oh.. I also should ask this one, now i noticed ; instead of this : www.mysite.com/ourband.php?page=contactus how can i do this one ? www.mysite.com/ourband/contact_us.html Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-603683 Share on other sites More sharing options...
obsidian Posted July 30, 2008 Share Posted July 30, 2008 When you're getting into specific occurrences like this, it's usually easier to manage specific white lists of things to match. Also, remember to always list your rules in order of most specific to most generic, so that they will cascade properly: RewriteRule ^ourband/contact_us(\.html)?$ /ourband.php?page=contactus [L] RewriteRule ^ourband/([a-zA-Z]+)/?$ /ourband.php?page=$1 [L] **EDIT** Come to think of it, you could very well combine different ones that refer to the same file, too: RewriteRule ^ourband/(contact_us|contactus|contact)(\.html?$ /ourband.php?page=contactus [L] This example should accept contact_us.html, contactus.html and contact.html as well. Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-603686 Share on other sites More sharing options...
Rommeo Posted July 30, 2008 Author Share Posted July 30, 2008 obsidian thank you so much !.. Now everything is working. At my first problem, now i did what you have said. Added this one into head tags. "<base href="http://www.mysite.com" />" Now all my links are working. Thank you again. i m wondering if there is any technique for the links like www.mysite.com/ourband/who_we_are.html i saw this at wordpress or something. So i think its about deleting the spaces in an url. Is it possible with mod_rewrite functions ? Or let me ask what's the technique for to do this ? Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-603696 Share on other sites More sharing options...
obsidian Posted July 30, 2008 Share Posted July 30, 2008 obsidian thank you so much !.. Now everything is working. At my first problem, now i did what you have said. Added this one into head tags. "<base href="http://www.mysite.com" />" Now all my links are working. Thank you again. Great! So glad it is working for you. i m wondering if there is any technique for the links like www.mysite.com/ourband/who_we_are.html i saw this at wordpress or something. So i think its about deleting the spaces in an url. Is it possible with mod_rewrite functions ? Or let me ask what's the technique for to do this ? This technique is actually slightly different. It is more of a combination mod_rewrite and PHP script. It's the receiving file that actually does the collection of data. For instance, you would have something as simple as this for your mod_rewrite: RewriteRule ^ourband/blog/([^./]+).*$ /ourband.php?page=blog&article=$1 [L] Notice how you would know that it's the blog page you are automatically targeting, but you are passing in the last argument as the article which is to be displayed. Then, your PHP script would see if the requested article was a viable request: <?php $a = isset($_GET['article']) ? $_GET['article'] : ''; $a = str_replace('_', ' ', $a); // Here is where you change your underscores to spaces // Set up the query string to retrieve the article they requested $q = sprintf("SELECT * FROM articles WHERE title = '%s' LIMIT 1", mysql_real_escape_string($a)); // Do the query and see if there is a matching article. // Handle any errors as you see fit. ?> Hope this little overview helps to clarify a little better the techniques you are seeking. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-603708 Share on other sites More sharing options...
Rommeo Posted July 30, 2008 Author Share Posted July 30, 2008 Now i see .. i ll try to use that technique. Thank you one more time obsidian (: ! Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-603715 Share on other sites More sharing options...
Rommeo Posted July 31, 2008 Author Share Posted July 31, 2008 Now i have another problem when i enter : www.mysite.com/myband/about_us.html it goes to main page which is a result of 404 error. when i enter : www.mysite.com/index.php?page=about_us it works. ( no problem with deleting "_" chars ) page=aboutus also works. when i enter : www.mysite.com/myband/aboutus.html it works. just www.mysite.com/myband/about_us.html not working. I think that's cause of we didnt declared "_" char at the htaccess file. but i m not sure. RewriteRule ^myband/([a-zA-Z]+)(\.html)/?$ index.php?page=$1 [L] this is the rule i m using. to use "_" what should i do ? Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-604123 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 I just replied to your PM, but I'll post here, too. Just update the regular expression character family to match underscores as well: RewriteRule ^myband/([a-zA-Z_]+(\.html)/?$ index.php?page=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-604565 Share on other sites More sharing options...
Rommeo Posted July 31, 2008 Author Share Posted July 31, 2008 obsidian ; You're the best ! Thank you (: it really means a lot to me. it's working now (: no problem with any links (: Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-604724 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 Glad to help Quote Link to comment https://forums.phpfreaks.com/topic/117369-solved-what-is-wrong-here/#findComment-604729 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.