Redirect using htaccess, use slash, underscore or date as delimiter - friendly url
P粉717595985
P粉717595985 2023-09-11 12:11:18
0
1
415

This works fine:

https://example.com/2023/not-the-dinner-day

Use the followingRewiteRule

RewriteRule ^(\d{4})/not-the-dinner-day/?$ archive/reports//ntdd.php [NC,L]

I want to get this new URL:

https://example.com/2023/not-the-dinner-day-photographs

From this old URL

https://example.com/archive/galleries/2023/20230204_ntdd/

I have tried various methods including:

RewriteEngine On RewriteRule ^(\d{4})/not-the-dinner-day-photographs/?$ archive/galleries//_ntdd/ [NC,L]

Match the last folder20230204_ntddseems to be causing the problem for me, the rewrite module is turned on.

P粉717595985
P粉717595985

reply all (1)
P粉180844619

Since the0204(MMDD) part does not exist in the requested URL, you need to hardcode it. Fortunately, there is only one MMDD in any given year.

If you have access to the server configuration, you can implement aRewriteMapthat contains a one-to-one mapping - that mapping can then be looked up in.htaccess.

Otherwise, you can do something like the following in.htaccess:

RewriteCond -0202 2019-(\d{4}) [OR] RewriteCond -0201 2020-(\d{4}) [OR] RewriteCond -0206 2021-(\d{4}) [OR] RewriteCond -0205 2022-(\d{4}) [OR] RewriteCond -0204 2023-(\d{4}) RewriteRule ^(\d{4})/not-the-dinner-day-photographs/?$ archive/galleries//%1_ntdd/index.php [NC,L]

The precedingconditionscontain a one-to-one mapping of YYYY to MMDD.$1is the 4 digit year captured fromRewriteRulepattern, thenCondPatternis captured from TestString, which is used The%1backreference in the generated replacement string is retrieved.


Narration:

However, there are some potential SEO issues with your original rules:

  1. You allow an optional trailing slash on the requested URL. But both will return the same resource. These are strictly two different URLs, and may createduplicate contentissues. If it's possible to receive two URLs at the same time (with or without a trailing slash), then ideally you should redirect from one URL to the other (regardless of specification).

  2. You allow case-insensitive matching. Again, this may causeduplicate contentissues. If you do have mixed case requests, then you should use external redirects to normalize/correct the requests. Please see the following:
    How to rewrite a URL from uppercase to lowercase in .htaccess

  3. If you are changing the existing URL structure (that has been indexed and/or linked to by a third party), then you need to implement an external 301 redirect in another directive, from old to new, in order to protect searches Engine optimization.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!