Reputation: 3
I'm trying to write clean url's for my website. I'm a rookie at this so forgive me. My .htaccess file currently looks like this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)$ something.php?query=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ something.php?query=$1
The first rule seems to work. For example website.com/good-looking-query-string
does in fact rewrite to website.com/something.php?query=ugly+looking+query+string
The second rule is where I'm having a problem. I can't get that trailing slash to work. For example website.com/good-looking-query-string/
appears to pull up the page but without any CSS rules applied. I noticed all the links end up appended to the query string also. For example the link back to index.php ends up like website.com/good-looking-query-string/index.php
I need to get that trailing slash to work. What in the world am I doing wrong?
Upvotes: 0
Views: 364
Reputation: 5223
Your issue is client side, you are using relative paths but you need to be using Root-relative
paths. A URL of:
website.com/good-looking-query-string
loads from the root. A url of:
website.com/good-looking-query-string/
loads from the good-looking-query-string
directory.
So instead of href="style.css"
you should have "href="/style.css"and the index issue should be
href="/index.php"instead of
href="index.php"`.
As noted in the earlier comment your regex could be simplified by adding a ?
on the trailing slash to make it option. That is not your issue though, just simplifies the rules.
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1
Upvotes: 0
Reputation: 542
You can combine two line in one line:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1
In this case /
will be optional
Upvotes: 0