3

OK guys, I have a problem that is literally driving me crazy. Here's what happened :

I decided that I wanted to rewrite the URL on my web-site.

It is supposed to rewrite from this syntax :

http://www.sample.com/programming.php?name=something

to this :

http://www.sample.com/tutorials/programming/something.php

Or (eg. 2) :

http://www.sample.com/other.php?name=test

to this :

http://www.sample.com/tutorials/other/test.php

So my URL syntax would be :

http://www.sample.com/tutorials/(name of my file)/(name of the variable).php

I have tried the following code :

RewriteEngine On
RewriteRule ^tutorials/programming/(.+)$ /programming.php?name=$1 [L]
RewriteRule ^tutorials/other/(.+)$ /other.php?name=$1 [L]

But, it doesn't rewrite the URL properly. The detailed explanation is below :

So, when I visit my original site, it appears like this :

http://www.sample.com/programming.php?name=something

If I visit this URL :

http://www.sample.com/tutorials/programming/something.php

I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :

http://www.something.com/index.php (Correct index.php URL)

but it's like this :

http://www.sample.com/tutorials/programming/index.php (which does not exist).

I have read over 10 tutorials online, asked my colleague to help me out, but neither did his solutions work. So, all I want to accomplish is that my URL is rewritten, so when the user choose a tutorial in programming, I don't get this URL in the address bar :

http://www.sample.com/programming.php?name=something

but this :

http://www.sample.com/tutorials/programming/something.php

and that is all I want.

I have tried to be as detailed as possible. If you need additional details, please, let me know.

Thanks in advance!

TheGhost
  • 301
  • 4
  • 12

2 Answers2

2

I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :

The relative/absolute paths you have in your page content is getting a different base because of the extra slash. When you have something like <img src="images/blah.gif">, the relative base is derived from the URL the browser sees (not what is internally re-written by the server). So if the browser sees: http://www.sample.com/programming.php?name=something, the URI is /programming.php and the URI base is /. But if the browser sees http://www.sample.com/tutorials/programming/something.php, the URI is /tutorials/programming/something.php and the URI base becomes /tutorials/programming/, which I'm assuming is not where your images/css/scripts/etc are located (since that directory probably doesn't even exist).

You need to either correct the URI base in all of your page headers by adding a:

<base href="/">

Or change all of your relative links to absolute links.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • I have done it and it worked! I can now access my full web-site (CSS + HTML) by using this URL : http://www.sample.com/tutorials/programming/something.php. However, when I go to my web-site I must manually enter previously mentioned URL to get that type of URL (the "new" URL is not shown in the address bar). My URL that is shown in my adress bar is still (so, I go normally on sample.com/index.php, and after that I click the link "Learn programming" and than click any tutorial): http://www.sample.com/programming.php?name=something. How to fix it? – TheGhost Sep 13 '12 at 20:10
  • @TheGhost That's an entirely different problem. You need to create a separate set of rules to address "links in the wild" (e.g. links that you have no control over, like from google). You just have to make sure that your internal linking is consistently the friendly URL. – Jon Lin Sep 13 '12 at 20:13
  • Just to ask now, is my URL now rewritten, despite the fact that it is not shown in the address bar? – TheGhost Sep 13 '12 at 20:15
  • @TheGhost See my [answer to this question](http://stackoverflow.com/a/12399668/851273). Redirecting and rewriting are 2 entirely different things. A rewrite happens internally on the server, the client is unaware of any kind of rewrites, rewrites are strictly for the server to handle requests and return content. Redirects affect the browser, they are not the same thing. So this **rewrite** will not affect the browser in any way whatsoever except for what content is returned. – Jon Lin Sep 13 '12 at 20:17
  • @TheGhost Sorry, ran out of space in previous comment: But to answer your question, Yes, it's being rewritten. But your browser will appear as if nothing happened because the rewrite happens strictly on the server, hidden from the client completely. – Jon Lin Sep 13 '12 at 20:20
  • Just to ask you one more thing. If I have rewritten the URL, is it now SEO-friendly, or would it be even more friendly if I toss in redirecting also? – TheGhost Sep 13 '12 at 20:21
  • @TheGhost The link in my comment has an example of what you need to do, but I don't know of any good tutorials personally. I know there's a lot of them on the internet. You could ask another question specifically about what URLs you want browsers to be redirected to, tailored to your needs. It's impossible to answer questions like that in comments since we can't format things – Jon Lin Sep 13 '12 at 20:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16663/discussion-between-theghost-and-jon-lin) – TheGhost Sep 13 '12 at 20:28
0

I get my web-site HTML, but without my CSS layout (just HTML displayed)

how did you include the css? did you include your files in the way

../../css/file.css

or in absolute mode

/css/file.css?

to check what really is the fault (i guess your rewrite success in your task eg i didn't understand it the right way) can you give us the real uri's?

donald123
  • 5,638
  • 3
  • 26
  • 23