Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache mod_rewrite syntax not working

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!

like image 284
TheGhost Avatar asked Apr 19 '26 19:04

TheGhost


1 Answers

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.

like image 146
Jon Lin Avatar answered Apr 21 '26 08:04

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!