2

I'm now using this .htaccess file. It only changes the URL from www.example.com/?page=home to www.example.com/home.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

RewriteRule ^([\w-]+)?$ index.php?page=$1 [L]
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L]

I want to add more $_GET parameter, for example www.example.com/home/parameter1/value1/. and preferably that you can do those parameters indefinitely.

1 Answer 1

3

You can't do this "indefinitely" using .htaccess alone. You would need to decide on the maximum number (N) of parameters and write a directive for each in order: N, N-1, N-2, ... 1. However, you are also limited by the number of backreferences that are supported. ie. With just $1 to $9, you are limited to 4 parameters, using your method of including the parameter name in the URL path as well.

For example:

# 3 additional parameters
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?page=$1&$2=$3&$4=$5&$6=$7 [L]

# 2 additional parameters
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?page=$1&$2=$3&$4=$5 [L]

# 1 additional parameter
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?page=$1&$2=$3 [L]

# No parameters (just the page)
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L]

These all allow an optional trailing slash (as in your current example). However, you are better off deciding whether a trailing slash is required or not and choosing one or the other. Having the trailing slash optional simply promotes duplicate content.

This URL pattern is also a bit too "generalised" in my opinion. It is more usual to be specific and avoid including the parameter name in the URL. For example: example.com/home/value1/value2/value2 - the parameter names (param1, param2, etc.) would be hardcoded in the RewriteRule substitution. This would also allow you to have twice as many parameters.

RewriteRule ^([\w-]+)?$ index.php?page=$1 [L]
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L]

You shouldn't need two directives here. (Providing the DirectoryIndex is set correctly.)

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

Comments

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.