0

I want to rewrite the URL of a page in my website. Its basicly really simple. My original URL looks like this

http://www.mypage.com/website/page.php?slug=my-page

I want it to look like this: http://wwww.mypage.com/website/my-page/

And that works. What doesent work is if you remove the trailing slash. This is my htaccess:

RewriteEngine on RewriteRule ^(.*)/$ page.php?slug=$1 [L] 

It seems as if i remove the slash the $_GET['slug'] becomes only page.php but with a trailing slash the variable says "my-page".

Is it possible to make it so the link works both without and with trailing slash?

Edit: Does it matter if i have the .htaccess and php file in a childfolder? So my real url is like this: http://www.mypage.com/website/page.php?slug=something

I've now edited the post with how it really is.

1
  • The location of the htaccess file makes a difference. Look up how htaccess works. Commented Dec 8, 2011 at 15:20

3 Answers 3

1

Try adding this:

RewriteRule ^website/([a-z0-9]+)/?$ website/page.php?slug=$1 [NC,L,QSA] 

Amend the regex as needed depending on the type of URLs you want to accept.

Look up how htaccess works with regard to things like querystrings and so on:

http://httpd.apache.org/docs/1.3/howto/htaccess.html

Sign up to request clarification or add additional context in comments.

2 Comments

@jaypCheck your code - you have an open paren and a closing square bracket atm!
Tried it again but didn't work, now i get a 404 not found. But look at my original post, i've updated it with some info. You see im not in the root folder of the domain. Does that effect anything?
1
RewriteEngine on RewriteRule ^(.*)/?$ page.php?slug=$1 [L] 

The question mark makes the trailing slash optional. I would also suggest to have only one kind of URLs. Like redirect URLs without the trailing salsh, to the one with.

Comments

1

I can only think of using this line before RewriteRule ^(.*)/$ page.php?slug=$1 [L]

RewriteRule ^(.*)$ page.php?slug=$1 [L] 

Basically, in your original code, you declared a trailing slash, so it requires a trailing slash

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.