Redirect certain subfolders by removing the parameter question mark

前端 未结 2 733
余生分开走
余生分开走 2021-01-23 07:30

I am using .htaccess to redirect certain subfolders of my domain, to remove the question mark to improve my URLs.

Currently my URLs are like this:



        
相关标签:
2条回答
  • 2021-01-23 08:06

    If only changing from query is your requirement then try with below, we are using QSD flag to discard our query string after our rule matched.

    RewriteCond %{QUERY_STRING} ([^\s&]+) [NC]
    RewriteRule ^ /post/%1 [R=302,L,NE,QSD]
    
    0 讨论(0)
  • 2021-01-23 08:21

    i am using php GET parameters, i am attempting for when the browser visits example.com/post/sometitle that the page that is currently example.com/post/?sometitle is displayed

    In that case you need to the opposite of what you are asking in your question: you need to internally rewrite (not externally "redirect") the request from example.com/post/sometitle to example.com/post/?sometitle.

    However, you must have already changed all the URLs in your application to use the new URL format (without the query string). You shouldn't be using .htaccess alone for this.

    I also assume that /post is a physical directory and that you are really serving index.php in that directory (mod_dir is issuing an internal subrequest to this file). So, instead of /post/?sometitle, it's really /post/index.php?sometitle?

    For example:

    RewriteEngine On
    
    # Rewrite /post/sometitle to filesystem path
    RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]
    

    So, now when you request /post/sometitle the request is internally rewritten and handled by /post/index.php?sometitle instead.

    I have assumed that "sometitle" can consist of 1 or more of the characters a-z, A-Z, 0-9, _ and -. Hence the regex [\w-]+.

    If this is a new site then you can stop there. However, if you are changing an existing URL structure that has already been indexed by search engines and linked to by external third parties then you'll need to redirect the old URLs to the new. (Just to reiterate, you must have already changed the URL in your application, otherwise users will experience repeated redirects as they navigate your site.)

    To implement the redirect, you can add something like the following before the above rewrite:

    # Redirect any "stray" requests to the old URL
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} ([\w-]+)
    RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]
    

    The check against the REDIRECT_STATUS environment variable is to ensure we only redirect "direct requests" and thus avoiding a redirect loop.

    (Change to 301 only when tested as OK, to avoid caching issues.)

    In Summary:

    RewriteEngine On
    
    # Redirect any "stray" requests to the old URL
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} ([\w-]+)
    RewriteRule ^post/$ /post/%1 [R=302,NE,QSD,L]
    
    # Rewrite /post/sometitle to filesystem path
    RewriteRule ^post/([\w-]+)$ /post/index.php?$1 [L]
    

    UPDATE: If you have multiple URLs ("folders") that all follow the same pattern, such as /post/<title>, /home/<title> and /build/<title> then you can modify the above to cater for all three, for example:

    # Redirect any "stray" requests to the old URL
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} ([\w-]+)
    RewriteRule ^(post|home|build)/$ /$1/%1 [R=302,NE,QSD,L]
    
    # Rewrite /post/sometitle to filesystem path
    RewriteRule ^(post|home|build)/([\w-]+)$ /$1/index.php?$2 [L]
    

    Aside: (With my Webmasters hat on...) This is not really much of an "improvement" to the URL structure. If this is an established website with many backlinks and good SE ranking then you should think twice about making this change as you could see a dip in rankings at least initially.

    0 讨论(0)
提交回复
热议问题