Forum Moderators: phranque
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.
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.
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]