Mod重写尾部斜杠问题

时间:2019-07-09 20:22:52

标签: php mod-rewrite

我有以下规则:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.Mail;
using DotNetNuke.Services.Social.Notifications;
using System.ComponentModel.Composition;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Entities.Tabs.Actions;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.FileSystem.EventArgs;
using DotNetNuke.Services.Log.EventLog;
using System.Net;

// For this I added a reference to the System.ComponentModel.Composition.dll
namespace TestProject.DotNetNuke.Entities.Users
{
    [Export(typeof(IUserEventHandlers))]
    public class UserEventHandlers : IUserEventHandlers
    {
        private void IUserEventHandlers_UserCreated(object sender, UserEventArgs args)
        {
            var x = "d";
        }

        private void IUserEventHandlers_UserDeleted(object sender, UserEventArgs args)
        {
            var x = "d";
        }
        public void IUserEventHandlers_UserAuthenticated(object sender, UserEventArgs args)
        {
            var x = "d";
        }

        public void IUserEventHandlers_UserUpdated(object sender, UpdateUserEventArgs args)
        {
            var x = "d";
        }

        private void IUserEventHandlers_UserRemoved(object sender, UserEventArgs args)
        {
            var x = "d";
        }

        private void IUserEventHandlers_UserApproved(object sender, UserEventArgs args)
        {
            System.Net.Mail.SendMail(args.User, MessageType.UserRegistrationPublic, PortalSettings.Current);
            DeleteAllNotifications(args.User.UserID);
        }

        private void DeleteAllNotifications(int userId)
        {
            var nt = NotificationsController.Instance.GetNotificationType("NewUnauthorizedUserRegistration");
            var notifications = NotificationsController.Instance.GetNotificationByContext(nt.NotificationTypeId, userId.ToString(CultureInfo.InvariantCulture));

            foreach (var notification in notifications)
                NotificationsController.Instance.DeleteNotification(notification.NotificationID);
        }
    }
}

然后在PHP中我有这个:

RewriteRule ^store/([^/]*)/([^/]*)/([^/]*)$ /products.php?param1=$1&param2=$2&param3=$3 [L]
RewriteRule ^store/([^/]*)/([^/]*)$ /products.php?param1=$1&param2=$2 [L]
RewriteRule ^store/([^/]*)$ /products.php?param1=$1 [L]
RewriteRule ^store/$ /products.php
RewriteRule ^store$ /products.php

现在使用此URL:

https://mywebsite.com/store/

$paramCount = 0; if(isset($_GET['param1']){ $param1 = $_GET['param1']; $paramCount++; } if(isset($_GET['param2']){ $param2 = $_GET['param2']; $paramCount++; } if(isset($_GET['param3']){ $param3 = $_GET['param3']; $paramCount++; } 应该为0,但应为1,但是使用此值:

https://mywebsite.com/store

$paramCount为0(正确)。

与此相同:

https://mywebsite.com/store/pc-parts/

$paramCount为2时应为1,但有一个:

https://mywebsite.com/store/pc-parts

$paramCount为1(正确)。

因此斜杠将其打破,我该如何解决?

1 个答案:

答案 0 :(得分:0)

经过一番尝试和错误后,我发现了这个问题,我颠倒了顺序,并在每条规则后添加了一个斜杠,以便它与第一个规则匹配:

RewriteRule ^store$ /products.php
RewriteRule ^store/$ /products.php
RewriteRule ^store/([^/]*)$ /products.php?param1=$1 [L]
RewriteRule ^store/([^/]*)/$ /products.php?param1=$1 [L]
RewriteRule ^store/([^/]*)/([^/]*)$ /products.php?param1=$1&param2=$2 [L]
RewriteRule ^store/([^/]*)/([^/]*)/$ /products.php?param1=$1&param2=$2 [L]
RewriteRule ^store/([^/]*)/([^/]*)/([^/]*)$ /products.php?param1=$1&param2=$2&param3=$3 [L]
RewriteRule ^store/([^/]*)/([^/]*)/([^/]*)/$ /products.php?param1=$1&param2=$2&param3=$3 [L]

不确定这是否是最好的解决方案,但它适用于我的情况