Jump to content

[SOLVED] What is wrong here ?


Rommeo

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 ?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.