1

I want to rewrite URLs as domain.com/page using .htaccess where page is a variable.

I am using the following code for it:

RewriteRule ^(.*)$ index.php?room_name=$1 [L]

But it isn't working as desired. I think it is making all URLs like domain.com/login or domain.com/register act as the variable page.

How do I rewrite the URL such that the variable page rewrites only for index.php?

Edit: If domain.com/page is not possible, domain.com/room/page is also acceptable.

Edit 2: I have been using the following URL rewrites for this purpose earlier:

RewriteRule ^teen index.php?room_name=teen [L]
RewriteRule ^singles index.php?room_name=singles [L]

But as these pages are supposed to be user generated it is not possible to write for every page. I just want to automate these URL rewrites.

1 Answer 1

3

If domain.com/page is not possible, domain.com/room/page is also acceptable

You can use this rule:

RewriteRule ^room/([\w-]+)/?$ index.php?room_name=$1 [L,NC,QSA]

Also if you're using relative URLs for css/js/images then add this just below <head> tag of your page's HTML: <base href="/" /> so that every relative URL is resolved from that base URL and not from the current page's URL.

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

6 Comments

I have done it but it's not working. Do the PHP include also need to be relative?
Check the URL: chatadda.com/singles & login using guest & then check the URL - chatadda.com/room/singles you will get the issue.
click on Login as guest
check chatadda.com/room/singles even after adding base and fixing all the JS errors, it's still not working
Did you check source code in browser? There is still no base tag
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.