Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule With optional parameter is not working

I not managed to set optional parameters

         

ludson

5:48 am on Apr 1, 2009 (gmt 0)

10+ Year Member



Come on .. is very confusing!

I have a .htaccess in the root folder of my site.
with the following rule:

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

This rule is working perfectly when access: [localhost...] it redirects to: index.php?page=any_page

with this rule, if I access: [localhost...] go to index the with parameter empty, and I treat it ok .. is working as I want.

Now I need an optional parameter, which must, if the user enters: [localhost...] (with or without a slash at the end) it redirects to: index.php?member=anything.

And if the user enters: [localhost...] redirect it to: index.php?member=anything&page=page

I tried using the following rule:

RewriteRule ^(([^/]+))?(/([^/]+)\.html)?$ index.php?membro=$1&pagina=$2

The first and second parameters are optional.
when I try to access the url: [localhost...]

and the index made a debug: print_r($_GET)

The result of debug is:

Array ( [member] => index.php [page] => index.php )

That is, ignoring the 2 parameters are passed, the correct result should be:

Array ( [member] => anything [page] => page )

If I access: [localhost...]
the result of debug is:

Array ([member] => index.php [page] => index.php) 

and if I access: [localhost...]
Gives page not found.

Summarizing all, need to:

By accessing the page: [localhost...] - redirect to: [localhost...]

By accessing the page: [localhost...] - redirect to: [localhost...]

By accessing the page: [localhost...] - redirect to: [localhost...]

By accessing the page: [localhost...] - redirect to: [localhost...]

Gave you to understand the confusion?

Tried but could not quite make it work
Thanks for the help.

jdMorgan

3:18 pm on Apr 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simplest and most robust solution is to use two rules instead of one. Put the two-parameter rule first.
You can use one rule if you insist, by making the first sub-pattern reject periods -- i.e. use ^[/.]+ -- but this would break if a period was present in the "directory name".

Jim

ludson

5:03 pm on Apr 1, 2009 (gmt 0)

10+ Year Member



Hello JdMorgan,
Initially I had thought to create three rules, but was not able to operate, and tried to solve everything with only one rule.

Initially my rules were:


#First:
RewriteRule ^([^/]+)/?$ index.php?member=$1

# second:
RewriteRule ^([^/]+)\.html$ index.php?page=$1

# Third:
RewriteRule ^([^/]+)/([^/]+)\.html$ index.php?member=$1&page=$2

The problem was resolved as follows:


# First: If the URL is: http://localhost/mysite/anything or http://localhost/mysite/anything/
RewriteRule ^([^/.]+)/?$ index.php?member=$1

# Second: If the URL is: http://localhost/mysite/page.html
RewriteRule ^([^/]+)\.html$ index.php?page=$1

# Third: If the URL is: http://localhost/mysite/anything/page.html
RewriteRule ^([^/.]+)/([^/]+)\.html$ index.php?membro=$1&pagina=$2

The only thing that changed the rule that I had created and informed the creation of the topic, is that for the first and third rule, the parameter $ 1 is a point after the bar.

The consideration that this change in regulation? Without the point, it was past the page name as a parameter (member = index.php) now, when you put the point, the parameters are passed correctly. Raised the point only in the First Rule, and the first parameter of the third rule.

I wonder what changed, what is the effect of "." the rule only to learn better.

Thanks for the help, and there is a topic that someone else can be helped.

jdMorgan

6:26 pm on Apr 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The point is not needed in the pattern of your third rule. But it is required in the first rule to prevent the first rule from matching requests for "page.html". It can also help to speed up finding the ".html" extension.

You should list your rules from most-specific-pattern to least-specific, and add an [L] flag to all of them:


#If the URL-path is: /anything/page.html but not /anything/more/page.html
RewriteRule ^([^/]+)/([^/.]+)\.html$ index.php?membro=$1&pagina=$2 [L]
#
# Second: If the URL-path is: /page.html but not /anything/page.html
RewriteRule ^([^/.]+)\.html$ index.php?page=$1 [L]
#
# If the URL-path is: /anything or /anything/ but not /anything/more or /anything.filetype
RewriteRule ^([^/.]+)/?$ index.php?member=$1 [L]

Jim