删除文件扩展名,但使用htaccess保持查询

时间:2015-06-10 18:42:11

标签: php apache .htaccess

我在给定的方向上有一个.htaccess"你好" 我想访问http://page.com/hello/file?id=4&whatever=5来访问其文件 但该文件实际上是http://page.com/hello/file.php?id=4&whatever=5

我的尝试是:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]

但是这似乎没有用,你有什么可能错的建议吗?

root htacess包含以下内容:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# BEGIN wtwp_cache
<IfModule mod_mime.c>

    # Text
    AddType text/css .css
    AddType application/x-javascript .js
    AddType text/html .html .htm
    AddType text/richtext .rtf .rtx
    AddType text/plain .txt
    AddType text/xml .xml

    # Image
    AddType image/gif .gif
    AddType image/x-icon .ico
    AddType image/jpeg .jpg .jpeg .jpe
    AddType image/png .png
    AddType image/svg+xml .svg .svgz

    # Video
    AddType video/asf .asf .asx .wax .wmv .wmx
    AddType video/avi .avi
    AddType video/quicktime .mov .qt
    AddType video/mp4 .mp4 .m4v
    AddType video/mpeg .mpeg .mpg .mpe

    # PDF
    AddType application/pdf .pdf

    # Flash
    AddType application/x-shockwave-flash .swf

    # Font
    AddType application/x-font-ttf .ttf .ttc
    AddType application/vnd.ms-fontobject .eot
    AddType application/x-font-otf .otf

    # Audio
    AddType audio/mpeg .mp3 .m4a
    AddType audio/ogg .ogg
    AddType audio/wav .wav
    AddType audio/wma .wma

    # Zip/Tar
    AddType application/x-tar .tar
    AddType application/x-gzip .gz .gzip
    AddType application/zip .zip
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On

    # Text
    ExpiresByType text/css A31536000
    ExpiresByType application/x-javascript A31536000
    ExpiresByType text/html A3600
    ExpiresByType text/richtext A3600
    ExpiresByType text/plain A3600
    ExpiresByType text/xml A3600

    # Image
    ExpiresByType image/gif A31536000
    ExpiresByType image/x-icon A31536000
    ExpiresByType image/jpeg A31536000
    ExpiresByType image/png A31536000
    ExpiresByType image/svg+xml A31536000

    # Video
    ExpiresByType video/asf A31536000
    ExpiresByType video/avi A31536000
    ExpiresByType video/quicktime A31536000
    ExpiresByType video/mp4 A31536000
    ExpiresByType video/mpeg A31536000

    # PDF
    ExpiresByType application/pdf A31536000

    # Flash
    ExpiresByType application/x-shockwave-flash A31536000

    # Font
    ExpiresByType application/x-font-ttf A31536000
    ExpiresByType application/vnd.ms-fontobject A31536000
    ExpiresByType application/x-font-otf A31536000

    # Audio
    ExpiresByType audio/mpeg A31536000
    ExpiresByType audio/ogg A31536000
    ExpiresByType audio/wav A31536000
    ExpiresByType audio/wma A31536000

    # Zip/Tar
    ExpiresByType application/x-tar A31536000
    ExpiresByType application/x-gzip A31536000
    ExpiresByType application/zip A31536000
</IfModule>
<FilesMatch "\.(?i:css|js|htm|html|rtf|rtx|txt|xml|gif|ico|jpg|jpeg|jpe|png|svg|svgz|asf|asx|wax|wmv|wmx|avi|mov|qt|mp4|m4v|mpeg|mpg|mpe|pdf|swf|ttf|ttc|eot|otf|mp3|m4a|ogg|wav|wma|tar|gz|gzip|zip)$">
    <IfModule mod_headers.c>
        Header set Pragma "public"
        Header append Cache-Control "public, must-revalidate, proxy-revalidate"
        Header unset ETag
    </IfModule>
</FilesMatch>
<FilesMatch "\.(?i:css|js|gif|ico|jpg|jpeg|jpe|png|pdf|swf|ttf|ttc|eot|otf)$">
    <IfModule mod_headers.c>
        Header unset Set-Cookie
    </IfModule>
</FilesMatch>
# END wtwp_cache

# BEGIN wtwp_security
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^wp-admin/includes/ - [F,L]
    RewriteRule !^wp-includes/ - [S=3]
    RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
    RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
    RewriteRule ^wp-includes/theme-compat/ - [F,L]
</IfModule>
<Files "wp-config.php">
    Order allow,deny
    Deny from all
</Files>
Options -Indexes
# END wtwp_security

3 个答案:

答案 0 :(得分:3)

使用QSA标志,您实际上不需要将其添加到RewriteRule中的替换中。这应该自动运行。

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L,QSA]

根据您的评论,您似乎可能需要RewriteBase。

Options -MultiViews 
RewriteEngine On 
RewriteBase /API 
RewriteCond %{QUERY_STRING} ^(.*)$ 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+?)/?$ $1.php [L,QSA]

编辑2:

如果你有wordpress规则,它往往会接受每个请求并路由它。因此,请尝试将其从wordpress规则中排除。然后在子文件夹.htaccess中使用上面的代码。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/subfolder [NC]
RewriteRule ^ - [L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

您在全文中发布的.htaccess规则。删除重复的wordpress规则,并替换为我修改过的上述单个规则。

答案 1 :(得分:1)

您不需要保存查询 - QSA保存它。但是为了避免无限重定向,你需要说Cond,请求还没有php扩展

RewriteEngine On
RewriteCond %{REQUEST_URI}  !\.php$
RewriteRule ^(.*)$ $1.php [NC,L,QSA]

答案 2 :(得分:0)

QSA表示查询字符串追加,因此它将自动显示:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
相关问题