【问题标题】:Rewrite php url variables with mod_rewrite htaccess [duplicate]用 mod_rewrite htaccess 重写 php url 变量 [重复]
【发布时间】:2017-01-22 20:15:32
【问题描述】:

我仍在围绕 htaccess 工作,并希望了解我的代码是否正确或需要一些工作。另外我知道有很多这样的问题,但仍然无法弄清楚,如何将 URL days/content.php?day=mon 重写为 days/mondays/content/mon?谢谢!

#---------------------------------------------------------
#rewrite engine + rewritecond
#---------------------------------------------------------
<IfModule mod_rewrite.c>
  # enable the rewrite engine
  Options +FollowSymlinks
  RewriteEngine On

  # Set your root directory
  RewriteBase /

  # To externally redirect /dir/foo.php to /dir/foo
  RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
  RewriteRule ^ %1 [R,L,NC]

  ## To internally redirect /dir/foo to /dir/foo.php
  RewriteCond %{REQUEST_FILENAME}.php -f [NC]
  RewriteRule ^ %{REQUEST_URI}.php [L]

  # remove index and reference the directory
  RewriteRule (.*)/index$ $1/ [R=301]

  # remove trailing slash if not a directory
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*)/ $1 [R=301]

  #prevent hotlinking
  RewriteCond %{HTTP_REFERER} !^$
  RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?dundaah.com [NC]
  RewriteRule \.(jpe?g|png|gif|bmp|css|js|php|xml)$ - [NC,F,L]

  #deny access to hidden files and directories
  RewriteCond %{SCRIPT_FILENAME} -d [OR]
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]

</IfModule>

#---------------------------------------------------------
#compress text files
#---------------------------------------------------------
<IfModule mod_deflate.c>

    # Force compression for mangled headers.
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>

    # Compress all output labeled with one of the following MIME-types
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE application/atom+xml \
                                      application/javascript \
                                      application/json \
                                      application/rss+xml \
                                      application/vnd.ms-fontobject \
                                      application/x-font-ttf \
                                      application/x-web-app-manifest+json \
                                      application/xhtml+xml \
                                      application/xml \
                                      font/opentype \
                                      image/svg+xml \
                                      image/x-icon \
                                      text/css \
                                      text/html \
                                      text/plain \
                                      text/x-component \
                                      text/xml
    </IfModule>

</IfModule>

#---------------------------------------------------------
#compress files with mod_gzip
#---------------------------------------------------------
<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
 
#---------------------------------------------------------
#set expires headers cache control 
#---------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                      "access plus 1 week"
#CSS
    ExpiresByType text/css                              "access plus 1 week"
#Data interchange
    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"
#Favicon (cannot be renamed!)
    ExpiresByType image/x-icon                          "access plus 1 week"
#HTML components (HTCs)
    ExpiresByType text/x-component                      "access plus 1 week"
#HTML
    ExpiresByType text/html                             "access plus 0 seconds"
#JavaScript
    ExpiresByType application/javascript                "access plus 1 week"
#Manifest files
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"
#Media
    ExpiresByType audio/ogg                             "access plus 1 week"
    ExpiresByType image/gif                             "access plus 1 week"
    ExpiresByType image/jpeg                            "access plus 1 week"
    ExpiresByType image/jpg                             "access plus 1 week"
    ExpiresByType image/png                             "access plus 1 week"
    ExpiresByType video/mp4                             "access plus 1 week"
    ExpiresByType video/ogg                             "access plus 1 week"
    ExpiresByType video/webm                            "access plus 1 week"
#Web feeds
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"
#Web fonts
    ExpiresByType application/font-woff2                "access plus 1 month"
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"
</IfModule>
 
#---------------------------------------------------------
#cache files
#---------------------------------------------------------
<ifModule mod_headers.c>
    <filesMatch ".(ico|jpe?g|png|gif|swf)$">
        Header set Cache-Control "public"
    </filesMatch>

    <filesMatch ".(css)$">
        Header set Cache-Control "public"
    </filesMatch>

    <filesMatch ".(js)$">
        Header set Cache-Control "private"
    </filesMatch>

    <filesMatch ".(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
    </filesMatch>
    
    #to disable for certain file type
    <FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
        Header unset Cache-Control
    </FilesMatch>
</ifModule>

#---------------------------------------------------------
#turn e-tags off
#---------------------------------------------------------
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>
FileETag None

#---------------------------------------------------------
#disable dir browsing + script execution
#---------------------------------------------------------
Options -Indexes -ExecCGI
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi

#---------------------------------------------------------
#block access to your .htaccess file + more
#---------------------------------------------------------
<Files .htaccess>
  order allow,deny
  deny from all
</Files>

#---------------------------------------------------------
#create custom error pages
#---------------------------------------------------------
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php

#---------------------------------------------------------
#display no php errors to user
#---------------------------------------------------------
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

#---------------------------------------------------------
# log php errors to file
#---------------------------------------------------------
php_flag log_errors on
#php_value error_log /location/to/php_error.log

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    如果您希望将 URL days/content.php?day=mon 重写为 days/mon,请考虑以下重写规则:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/days/content.php
    RewriteCond %{QUERY_STRING} ^day=([a-zA-Z]+)$
    RewriteRule ^(.*)$ /days/%1? [R,L]
    

    或者,如果您希望将其重写为 days/content/mon,请考虑以下:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/days/content.php
    RewriteCond %{QUERY_STRING} ^day=([a-zA-Z]+)$
    RewriteRule ^(.*)$ /days/content/%1? [R,L]
    

    希望对你有帮助!

    【讨论】:

    • 谢谢但还是不行,给我Not Found
    • 您正在尝试从该 URL days/content.php?day=mon 重定向到 days/mon。对吗?
    • 是的,这就是我想要的。第一个代码给我一个 404 错误,第二个代码给我一个 500 错误。
    • 您已将它们写在 &lt;IfModule mod_rewrite.c&gt; 指令中。对吗?
    • 是的,第一个代码在浏览器 url 中显示days/mon,但显示我的自定义 404 页面。我正在使用htaccess,就像我在这里发布的那样
    【解决方案2】:

    你只需要使用这个:

    RewriteEngine On
    RewriteRule ^days/([^/]*)$ /days/content.php?day=$1 [L]
    

    它会给你留下 URL:www.example.com/days/mon

    确保在测试之前清除缓存。

    【讨论】:

    • 谢谢,但仍然无法正常工作,浏览器中的 url 仍为 days/content.php?day=mon 但我收到 500 错误
    【解决方案3】:
    RewriteCond %{SCRIPT_FILENAME} !-f    
    RewriteRule ^days\/(.+)$ days/content.php?day=$1 [NC,L]
    

    将从days/mon 重定向到days/content.php?day=mon

    如果您收到 not found 错误,可能是因为RewriteBase /。默认情况下,.htaccess url 是相对于它所在的目录的。通过将 base 设置为 /,url 然后变为相对于 root。

    对于下面的例子,我删除了RewriteBase

    <IfModule mod_rewrite.c>
        Options +FollowSymlinks
        RewriteEngine On    
    
        # This shouldn't be required, as it defaults to the directory the .htaccess is in
        # RewriteBase /
    
        # rewrite days/[day] into days/content.php?day=[day]
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteRule ^days\/(.+)$ days/content.php?day=$1 [NC,L]
    
        ...
    </IfModule>
    

    【讨论】:

    • 谢谢,但还是不行,day=mon 没有设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    相关资源
    最近更新 更多