.htaccess URL 重写挑战

发布于 2024-12-27 21:06:48 字数 1647 浏览 2 评论 0原文

我在某些 URL 重写方面遇到问题。

下面的所有内容都工作正常,但我需要添加一条从 URL 中删除查询字符串的规则。

site.com/page?a=b 将成为 site.com/page

有人可以帮忙吗?我已经阅读了一些关于 .htaccess 的内容,但我发现它非常复杂。另外,需要知道我的新指令应该出现在文件中的哪个位置。

谢谢。

    # EE 404 page for missing pages
    ErrorDocument 404 /index.php/404/index

    # Simple 404 for missing files
    
      ErrorDocument 404 "File Not Found"
    

    # Rewriting will likely already be on, uncomment if it isnt
    
    RewriteEngine On
    RewriteBase /
    

    # Block access to "hidden" directories whose names begin with a period. This
    # includes directories used by version control systems such as Subversion or Git.
    
      RewriteRule "(^|/)\." - [F]
    

    # remove the www - Uncomment to activate
    #
    #  RewriteCond %{HTTPS} !=on
    #  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    # 

    # Remove the trailing slash to paths without an extension
    # Uncomment to activate
    # 
    #   RewriteRule ^(.*)/$ /$1 [R=301,L]
    # 

    # Remove index.php
    # Uses the "include method"
    # http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
    # 
    RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/(home|inc|publishers|sidebars|about|include-template|testing|advertisers|products|sitemap|style|ad-choices|social-bar|search|404||members|P[0-9]{2,8}) [NC]
    RewriteRule (.*) /index.php/$1 [L]

I'm having trouble with some URL rewriting.

All of the stuff below works fine, but I need to add a rule which removes querystrings from URLS.

site.com/page?a=b
will become
site.com/page

Can someone help out? I have done some reading on .htaccess but I find it terribly complex. Also, will need to know where in the file my new directives should appear.

Thanks.


    # EE 404 page for missing pages
    ErrorDocument 404 /index.php/404/index

    # Simple 404 for missing files
    
      ErrorDocument 404 "File Not Found"
    

    # Rewriting will likely already be on, uncomment if it isnt
    
    RewriteEngine On
    RewriteBase /
    

    # Block access to "hidden" directories whose names begin with a period. This
    # includes directories used by version control systems such as Subversion or Git.
    
      RewriteRule "(^|/)\." - [F]
    

    # remove the www - Uncomment to activate
    #
    #  RewriteCond %{HTTPS} !=on
    #  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    # 

    # Remove the trailing slash to paths without an extension
    # Uncomment to activate
    # 
    #   RewriteRule ^(.*)/$ /$1 [R=301,L]
    # 

    # Remove index.php
    # Uses the "include method"
    # http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
    # 
    RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/(home|inc|publishers|sidebars|about|include-template|testing|advertisers|products|sitemap|style|ad-choices|social-bar|search|404||members|P[0-9]{2,8}) [NC]
    RewriteRule (.*) /index.php/$1 [L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2025-01-03 21:06:48

这将从 url 中删除查询字符串

RewriteRule ^(.*) /index.php/$1? [L] #remove query string

希望有帮助

This would remove query string from url

RewriteRule ^(.*) /index.php/$1? [L] #remove query string

Hope it helps

血之狂魔 2025-01-03 21:06:48

我的答案有点晚了,但由于我正在寻找类似的行为,我想我应该分享它:您还可以向重写规则添加一个标志以删除查询字符串;该标志是 [QSD],它有助于避免像末尾的 ? 这样的解决方法;-)

在这里您可以找到有关此标志的更多信息。我想指出“这个标志在 [Apache] 版本 2.4.0 及更高版本中可用”

I'm a little late for an answer, but since i was searching for a similar behaviour, i thought i should share it: you can also add a flag to a rewrite rule to remove the query string; the flag is [QSD], and it helps avoiding workarounds like the ? at the end ;-)

Here you can find more about this flag. I feel like pointing out that "This flag is available in [Apache] version 2.4.0 and later"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文