Jump to content

Recommended Posts

Here is what I am trying to do.

 

Get from this

http://mysite.com/post.php?id=12&title=postname

 

to this

http://mysite.com/post/12/postname/

 

 

here’s the rewrite rule.

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^post/([a-zA-Z]+)/([0-9]+)/$ post.php?id=$1&title=$2

This is my html link that links to the post.php page.

<a href="post.php?id=12&title=postname">
	Click here to see the post!
</a> 

It does not change the url. 

 

 

I have also tried it like below and it gives me an internal error.

<a href="post/12/postname">
	Click here to see the post!
</a> 

Can you see what I have done wrong?  Also if the above method for the linking is correct, how do I get the "id" and "title" using the $_GET?

 

 

You have to change your own links so the second form is what you should use - not the post.php one.

 

1. The link should be absolute, so /post/12/postname (note the leading slash)

2. Your RewriteRule requires a trailing slash so either you remove that or you put the slash in your URLs

 

If you still get an Internal Server Error, try

Options +FollowSymLinks -MultiViews

You have to change your own links so the second form is what you should use - not the post.php one.

 

1. The link should be absolute, so /post/12/postname (note the leading slash)

2. Your RewriteRule requires a trailing slash so either you remove that or you put the slash in your URLs

 

If you still get an Internal Server Error, try

Options +FollowSymLinks -MultiViews

 

 

 

Here is the updated link with the added slash.

<a href="/post/12/postname">
	Click here to see the post!
</a> 

The url does change now in the url field. It looks like this. http://localhost/post/12/postname, when it should look like this http://localhost/websitename/post/12/postname   It does not give me the internal error but it gives me a different error. 

"The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error."

Edited by helloworld001

Then you need to account for the "websitename". Best would be rebasing the site into that folder (ie, setting the DocumentRoot to be /path/to/websitename) or else you'll need to track that value somewhere and make sure you output it every time you generate URLs.

Because you can't do relative paths (eg, just "post/12/postname") all the time: on the /websitename/post/12/postname page, trying that link again would take you to /websitename/post/12/post/12/postname.

Then you need to account for the "websitename". Best would be rebasing the site into that folder (ie, setting the DocumentRoot to be /path/to/websitename) or else you'll need to track that value somewhere and make sure you output it every time you generate URLs.

Because you can't do relative paths (eg, just "post/12/postname") all the time: on the /websitename/post/12/postname page, trying that link again would take you to /websitename/post/12/post/12/postname.

 

Here is the new link.

<a href="/websitename/post/12/postname">
	Click here to see the post!
</a> 

It appears as  in the url and it gives an internal server error.

 http://localhost/websitename/post/12/postname.

How do I set up the DocumentRoot path in the .htaccess?

Edited by helloworld001

It appears as  in the url and it gives an internal server error.

Did you do that thing I said to do if you kept getting that error?

 

How do I set up the DocumentRoot path in the .htaccess?

Unless you want to support multiple sites at once, just put all the files you currently have in websitename/ in the parent directory (htdocs or /var/www or whatever that is).

If you do want multiple projects/sites on your dev box, create a virtual host and you don't need to muck around with that stuff. Each project should have one. Then the url could just be "www.yoursite.dev" or whatever you want the domain name to be on your dev server instead of "localhost/projectname". Then when you create absolute links like "/post/12/postname" it would be correct, like www.yoursite.dev/post/12/postname. You also wouldn't need to alter settings between the dev and live servers.

If you do want multiple projects/sites on your dev box, create a virtual host and you don't need to muck around with that stuff. Each project should have one. Then the url could just be "www.yoursite.dev" or whatever you want the domain name to be on your dev server instead of "localhost/projectname". Then when you create absolute links like "/post/12/postname" it would be correct, like www.yoursite.dev/post/12/postname. You also wouldn't need to alter settings between the dev and live servers.

Understood. I will try that.

 

Thanks.

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.