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

This works fine:

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

Use the following RewiteRule

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_ntdd seems to be causing the problem for me, the rewrite module is turned on.

P粉717595985
P粉717595985

reply all(1)
P粉180844619

Since the 0204 (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 a RewriteMap that 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 preceding conditions contain a one-to-one mapping of YYYY to MMDD. $1 is the 4 digit year captured from RewriteRule pattern, then CondPattern is captured from TestString, which is used The %1 backreference 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 create duplicate content issues. 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 cause duplicate content issues. 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