0

So im trying to do something simple, just want to change the urls like for example:

website.com/user?slug=usernameexample to website.com/user/usernameexample and I used the code below:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?user/([^/d]+)/?$ user.php?slug=$1 [L,QSA]

Seems simple but for my surprise this is a little nightmare because in the url website.com/user/usernameexample all files like css/js and others will not work because will try to open inside the directory "user" And thats not all... Also all links and files will try to the same, so for example login.php will also try to open inside the "user" folder that does not exist...

After reading many stackoverflow answers seems that the most common answer is "Absolute Paths" or <base href="http://www.example.com/" />... However The "base href" answer don't really works properly because that brings other issues... So seems that "Absolute Paths" are the only viable answer?

I wonder If there is another solution... I did tried some small htaccess changes but they did not worked with me...

  • How are you referencing your CSS and JS files in your PHP scripts as well as other PHP scripts? – Dave Nov 24 '19 at 11:46
  • @Dave something like href="assets/css/style.css" – ramenknight Nov 24 '19 at 11:48
  • 2
    No "small changes" are going to fix the underlying issue, that `/user/` _is_ the current path from the client's perspective, so it will resolve _all_ relative URLs in relation to that. If you do not want to specify a `base` element (not a fan of it personally either), then starting all your relative references with `/` is the most common and viable solution. If that also brings up "other issues" for you, then explain what those are. – 04FS Nov 24 '19 at 14:17
  • `!-f`/`!-d` test if the file or dir exist on disk, if yes the following rule will **not** be processed. If your CSS/JS exist on disk, this rule will **not** affect them. KISS - your regexp says the leading `/user/` is optional, which means it would match *any* request. Try: `user/(.+) user.php?slug=$1 [L,QSA]`. [There really](https://stackoverflow.com/questions/25080835/pretty-urls-with-htaccess) are [so many](https://stackoverflow.com/questions/25080835/pretty-urls-with-htaccess) duplicates [here on SO](https://stackoverflow.com/questions/14546712/how-to-use-htaccess-for-beautiful-links). – Don't Panic Nov 24 '19 at 14:17
  • Does this answer your question? [How can I create friendly URLs with .htaccess?](https://stackoverflow.com/questions/3033407/how-can-i-create-friendly-urls-with-htaccess) – Don't Panic Nov 24 '19 at 14:19
  • @Don'tPanic this is not about CSS/JS requests being rewritten by the rule on the server side, this is about changing the base path the client uses to complete relative URLs - what the client currently requests does _not_ exists as a file, _because_ the path is wrong. – 04FS Nov 24 '19 at 14:19
  • @04FS OP thought his rewrite rule was messing his CSS/JS, and so wasn't a viable solution. I'm pointing out that's not true. – Don't Panic Nov 24 '19 at 14:57
  • @04FS I think you are correct, seems that there is no "quick fix" with this... – ramenknight Nov 25 '19 at 09:48
  • @Don'tPanic the rewrite is breaking everything, including the website functionality, not just files... Seems that starting all relative references with / is the only solution... :/ – ramenknight Nov 25 '19 at 09:54
  • @ramenknight Oh - you are talking about your CSS/JS references in your HTML? I didn't catch that at all. Well, yes, @04FS is right, unless you want to go adding more rewrite rules that for your CSS/JS, but that's not a good option. If by `breaks everything` you mean it is also affecting non-CSS/JS pages, for eg the `login.php` example you mention in the question, see my previous comment about your regexp making `/user/` optional for why that is happening and how to fix it. – Don't Panic Nov 25 '19 at 10:06

1 Answers1

0

Yes, Absolute Paths are the only viable answer.