简体   繁体   中英

IIS Rule Convert To .htaccess Rule

I need some help writing a .htaccess rule that:

  1. Removed the .php extension from a file name
  2. Rewrites variables.

So, a url like www.example.com/contact-us/456/789 would be interpereted by the server as www.example.com/contact-us.php?a=123&b=456&c=789

The following IIS web.config rule works a treat (using a rewrite map):

<rule name="Dynamic Rewriting" stopProcessing="true">
    <match url="^([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{DynamicRewriting:{R:1}}" pattern="(.+)" />
    </conditions>
    <action type="Rewrite" url="/{C:1}.php?page={C:1}&amp;a={R:2}&amp;b={R:3}&amp;c={R:4}" />
</rule>

<rewriteMap name="DynamicRewriting">
    <add key="contact-us" value="contact-us" />
</rewriteMap>

Can anyone show me the .htaccess rule I need?

Try:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^/?([^/]+)(?:/?([^/]+)?)(?:/?([^/]+)?)(?:/?([^/]+)?)/?$ /$1.php?page=$1&a=$2&b=$3&c=$4 [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM