10945

.htaccess mod rewrite 301-redirect

Question:

I want: all links which not contained filename (not .html, .jpg, .png, .css) redirect with state 301 to directory, for example: <a href="http://mysite.com/article" rel="nofollow">http://mysite.com/article</a> -> <a href="http://mysite.com/article/" rel="nofollow">http://mysite.com/article/</a> But <a href="http://mysite.com/article/article-15.html" rel="nofollow">http://mysite.com/article/article-15.html</a> not redirects. What regulat expression I must write to .htaccess for adding slash to virtual directories?

Answer1:

I think the following might work:

RewriteEngine on RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$ RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

When it comes to mod_rewrite I can never be sure without testing though...

Answer2:

Clarification needed:

Given the url: <a href="http://server/path/file" rel="nofollow">http://server/path/file</a>

Does that get redirected to: <a href="http://server/path/" rel="nofollow">http://server/path/</a>

Or does it get redirected to: <a href="http://server/path/file/" rel="nofollow">http://server/path/file/</a>

As in: Do you want the redirects to go to the parent path, or do you just want to add a slash and assume directory out of the current path?

Answer3:

MB's RewriteRule above will fail on paths like /a because it needs to match at least two characters after the slash. Moreover it only matches on top directory URLs.

RewriteRule ^(([^\/]+\/)*[^\/\.]+)$ http://%{HTTP_HOST}/$1/ [R=301,L]

Is the purpose of this to reduce history pollution/false negatives?

Recommend

  • .htaccess - Remove www, force https, remove php and remove trailing slash
  • htaccess: domain hosted on subdirectory
  • How to combine Apache redirects?
  • Rewrite rule for subfolder
  • htaccess internal rewrite from subdomain to subdir path
  • Redirect site with .htaccess but exclude two+ folders
  • mod rewrite subdomain to folder break paths to css and images
  • Remove two subdirectories from url with mod_rewrite
  • SilverStripe. How to redirect from www.domainName to domainName?
  • C# regex for assembly style hex numbers
  • Nginx custom PHP error_pages with different header status codes and body contents
  • How can I .htaccess 301 redirect all pages/files (except for one) to new domain?
  • Check for stdout or stderr
  • Why does my regular expression fail with certain substitutions?
  • Why does _GET in PHP wrongly decodes slash?
  • Working example of paypal Website Payments Pro Hosted Solution Iframe?
  • .htaccess mod rewriterule and ampersands
  • How to run requests.get asynchronously in Python 3 using asyncio?
  • Help with mod_rewrite
  • htaccess settings for angularJS pushstate
  • Removing the .php file extension from the URL Request
  • System call time out?
  • Redirect to trailng slash (htaccess)
  • Angular page doesn't refresh after data is added or removed
  • Haskell: function composition with anonymous/lambda function
  • SonarQube: Cannot deactivate rule with missing quality profile
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • How do I superscript characters in a UIButton?
  • Zurb Foundation _global.scss meta styles for js?
  • htaccess add www if not subdomain, if subdomain remove www
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • FFmpeg Conversion Error
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • How to add date and time under each post in guestbook in google app engine
  • coudnt use logback because of log4j