Home > Enterprise >  URL Rewrite only main request and ignore subfolders
URL Rewrite only main request and ignore subfolders

Time:10-21

I have moved my Forum from vb4 (with vbseo) to vb5...

my old vb4 urls are:

example.com/123-threadtitle

new vb5 is:

example.com/category/subcategory/123-threadtitle 

123 is the id of the thread.

my try:

RewriteRule ([0-9] ) -[^*]  hxxps://www.example.com/showthread.php?t=$1 [L,R=301]

it works but he rewrite the vb5 requests again and we have problem.

how can I set it to ignore foldered requests?

RewriteCond %{REQUEST_FILENAME} !-d

is not working :-(

CodePudding user response:

RewriteRule ([0-9] ) -[^*]  hxxps://www.example.com/showthread.php?t=$1 [L,R=301]

The regex (RewriteRule pattern) is not anchored so matches anywhere in the requested URL-path. The regex does not exclude slashes (ie. path segment delimiters) so will consume the entire URL-path.

If your old URLs are of the form /123-threadtitle then you would seem to only need to match the first path segment, which should naturally exclude "subfolders" (by which I think you are referring to the additional path-segments in the new URL format, not physical directories, which is what the condition you posted checks for).

Try the following instead:

# Redirect old URLs to "/showthread.php?t=NNNN"
RewriteRule ^(\d )-[^/] $ https://www.example.com/showthread.php?t=$1 [L,R=301]

This matches /123-threadtitle, but not /category/subcategory/123-threadtitle and externally redirects the request to /showthread.php?t=123.

This needs to go near the top of the .htaccess file before any existing rewrites. Test with a 302 (temporary) redirect first to avoid potential caching issues.


However, for SEO purposes it may be preferable to internally rewrite the request to /showthread.php?t=123 instead and then trigger an external redirect to the new URL (ie. /category/subcategory/123-threadtitle) in your PHP code.

# Internally rewrite old URLs to "/showthread.php?t=NNNN"
RewriteRule ^(\d )-[^/] $ showthread.php?t=$1 [L]
  • Related