In Htaccess, I'D Like to Replace Underscores with Hyphens and Then Redirect the User the New Url

In htaccess, I'd like to replace underscores with hyphens and then redirect the user the new url

See the "Next" directive/rewrite flag, which causes the ruleset to be reevaluated starting with the first rule: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_n

Something like this might work:

RewriteRule (.*)_(.*) $1-$2 [N] 

Htaccess recursively replace underscores with hyphens

One of these implementations will cause 500 internal error if there are more than 10 underscores to be replaced by hyphen since default value of LimitInternalRecursion variable is 10.

Here is one way you can make these replacements work for 20 underscores:

RewriteEngine On

# when there are more than one _ then "recursively" replace it by -
RewriteRule ^([^_]*)_+([^_]*_.+)$ $1-$2 [N,DPI]

# when there is only one / then replace it by _ and redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [NE,L,R=301]

Reason why it works for more than 10 replacements because we are using N instead of L flag in first rule that results in improving the overall performance.

htaccess file - change underscores to hyphens for more than two words in url

Rather than so many complex rules, you can use these two recursive rules to replace all underscore by hyphens:

RewriteEngine On

RewriteRule \.(html?|php)$ - [L,NC]

RewriteRule ^([^_]*)_+([^_]*)$ http://www.askapache.com/$1-$2 [L,R=301,NE]

RewriteRule ^([^_]*)_+(.*)$ $1-$2 [N,DPI]

301 Redirects Rewrite Rule to replace underscore with hyphen and remove subdirectory

Answer from @Walf is close but requires some changes e.g. regex anchors and DPI flag.

You can use these rules on top of your site root .htaccess:

RewriteEngine On

# remove /sub_directory/ when there is no _ left
RewriteRule ^sub_directory/([^_]+)$ /$1 [R=301,NC,NE,L]

# use recursion based rule to replace _ by -
RewriteRule ^(sub_directory/[^_]*)_+(.*)$ $1-$2 [NC,N,DPI]

# rest of your rules go here

replace underscores with hyphens on one parameter only and then 301 in htaccess

The below solution works with no limit on the number on underscores. The first RewriteRule replaces the first underscore seen and the [N] flag causes processing to start over, so the rule will keep being applied until all the underscores are replaced. When no more underscores are left, the redirect is issued.

RewriteCond %{QUERY_STRING} ^state=([a-zA-Z_]+)&city=([a-zA-Z-]+)_([a-zA-Z_-]+)$
RewriteRule city.html /city.html?state=%1&city=%2-%3 [N]

RewriteCond %{QUERY_STRING} ^state=([a-zA-Z_]+)&city=([a-zA-Z-]+)$
RewriteRule city.html /find/this/in/%2/%1/? [L,R=301]

Replacing underscores with hyphens in .htaccess but stuck in a loop

Your rules are almost correct. Issue is that Apache is appending original path info to rewritten URI and it is going into an infinite loop (due to flag N in place).

You need to add DPI flag (Discard Path) with N to stop this behavior.

After little bit of refactoring your rules can be:

# redirect when we have only one underscore in URI
RewriteRule ^blog/archives/(\d{4}/\d{2}/\d{2})/([^_]*)_([^_]*)\.html$ /blog/$1/$2-$3/ [R=301,NC,NE,L]

# otherwise keep replacing underscore with hyphen in a loop
RewriteRule ^(blog/archives/\d{4}/\d{2}/\d{2})/([^_]*)_([^_]*_.*\.html)$ $1/$2-$3 [N,NC,DPI]

htaccess replace _ with -

Okay, so if I get your request correctly you are calling: mydomain.com/my-profile then the following will work:

RewriteEngine On

RewriteRule ^([^-]*)-([^-]*-.*) $1_$2 [N]
RewriteRule ^([^-]*)-([^-]*)$ index.php?m=$1_$2 [L]

Url redirection - changing folder, replacing underscores with dashes and removing html extension

I had a solution for this before, but it is breaking on my current setup (Apache crashes), and so it would be unwise of me to recommend it for your case. (It could be an issue with my setup, but I'd prefer to give you a more straight-forward route.)

This solution involves sending the relevant requests to a PHP file that will do the necessary replacements, and redirect once only. Note that your current implementation will send multiple redirect instructions to the browser. This is not only bad from a user experience point of view, but also from an SEO one.

To implement the solution, start by replacing your .htaccess directives with this:

RewriteEngine On

# Rewrite news and portfolio links to redirect.php
RewriteRule ^(news|portfolio)/(.+).html /redirect.php [L]

Then, create a redirect.php file in the same directory as your .htaccess file (in this case, your document root), and fill it with this simple replacement method and redirect instruction:

<?php

$path = $_SERVER['REQUEST_URI'];

# Perform the necessary replacements. The first array contains
# what we're searching for, bit by bit, and the second array
# contains the relevant replacements.
$path = str_replace(
['_', '/news/', '/portfolio/', '.html'],
['-', '/post/', '/project/', ''],
$path);

# Now, simply redirect to the new path.
# Change 302 to 301 use a "Moved Permanently" header,
# resulting in browsers and search engines caching
# the redirect.
header("Location: $path", true, 302);


Related Topics



Leave a reply



Submit