3

I am trying to achieve that a url like

example.com/path1 to example.com/index.php?p1=path1

example.com/path1/path2 to example.com/index.php?p1=path1&p2=path2

In my .htaccess I have:

RewriteEngine On

RewriteRule ^(.*)/(.*)$ /index.php?c=$1&g=$2 [NC,L]

RewriteRule ^([a-zA-Z0-9-_]+)$ /index.php?g=$1 [NC,L]

It works well for the case where we have the example.com/path1/path2 but for the the case example.com/path1 only works if there is no dot in the url. For example, I want to example.com/mydomain.com working to example.com/index.php?g=domain.com but I am not able to make it working.

Could you help me with this please?

2
  • 1
    with ([a-zA-Z0-9-_]+) you are explicitly only handling the case of alpha-numeric plus underscores. That would exclude your sample case. Commented Jul 25, 2018 at 15:45
  • Possible duplicate of htaccess RewriteRule problem Commented Jul 25, 2018 at 15:46

1 Answer 1

7

here's how I would handle this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1

then with whatever serverside language you're using I'll use PHP parse $_GET['param'].

$params = explode('/', $_GET['params']);
Sign up to request clarification or add additional context in comments.

2 Comments

You don't actually even need to pass through the params var on the rewrite, just pick it up with $_SERVER['REQUEST_URI'] - which makes the rule a little more succinct RewriteRule . /index.php [L,QSA]
Thank you very much, it worked and I have solved this way!

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.