.htaccess regex url redirection - apache

I'm looking for a regular expression for a .htaccess file where I can redirect one URL to another.
URL1:
/dir/an-article-title-222
URL2:
/dir/222/an-article-title
I would like URL1 to 301 redirect to URL2.
In highlevel language, I think I want to tell .htaccess to:
Take the digits after the last dash (-)
Put those digits before the article name with a trailing slash, i.e. 222/
Remove the last dash and digits from the article name
Any thoughts, suggestions, code are welcome!

Try using this rule :
RewriteEngine on
RewriteRule ^dir/([^/]+)-([0-9]+)$ /dir/$2/$1 [L,R=301]

RewriteRule ^/dir/([\w\d-]*)-([\d]+)$/ /dir/$2/$1 [L,R=301]
Try this, keep in mind that an-article-title by this regex allow a-z, 0-9 and -, for more chars edit the the part [\w\d-]

Related

htaccess URL rewrite page.php?page=string containing a hyphen

I need a solution for my redirect problem. In .htaccess file I have this code:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+) category.php?page=$1 [NC,L,QSA]
And this is working for a query like
category.php?page=pinturas and redirect to category/pinturas.
But, if the string have - like this:
category.php?page=aquoso-madeira it redirects to category/aquoso
Any help?
category.php?page=aquoso-madeira It redirect to category/aquoso
Because your RewriteRule pattern does not include the hyphen (-), so it matches everything up to, but excluding, the first hyphen.
Include the hyphen in the character class:
RewriteRule ^category/([a-zA-Z0-9-]+) category.php?page=$1 [NC,L,QSA]
Note that the hyphen (-) must go at the start or end of the character class, since this is a special character and has alternative meaning when used elsewhere.
And this working for query like category.php?page=pinturas and redirect to category/pinturas
Note that this rewrite does the complete opposite of what you are describing.
This rule takes a request like category/pinturas and internally rewrites it to category.php?page=pinturas. (There is no external redirect here - which might be in another part of your code?)

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

I need help creating a rewrite rule/301 redirect for the following link structures in the .httaccess folder.
Source URL: www.example.com/sub_directory/product_name_1.html
Destination URL: www.example.com/prodcut-name-1.html
The requirements for the redirect then are as follows:
Remove /sub_directory/
Change all underscores '_' to hyphens '-'
Unfortunately my regex isn't very good. I've tried searching around and but the solutions from other post with similar issues where not working for me (such as here)
Any help on a solution for this would be much appreciated. Also if you could please explain the why/how of it. I'd like to be able to better understand this.
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
Something like this
RewriteEngine on
RewriteBase /
RewriteRule ^(sub_directory/[^_]*)_+(.*) $1-$2 [DPI,N]
RewriteRule ^sub_directory(?:$|/(.*)) http://%{HTTP_HOST}/$1 [R=301,L]
It first loops through URLs that are in that subdirectory, to replace consecutive underscores with a single hyphen. It's done first so it doesn't interfere with other URLs that may contain an underscore. It then externally redirects (the cleaned) requests for that subdirectory to the root. The ugly grouping makes sure it only applies to exactly that folder.

If in htaccess for 301 Redirect special characters anywhere in URL to another characters?

Rule: Redirecting all URLs that contains %252C+ to ,- and redirecting + to - only if the + comes with %252C+.
e.g1: http://www.example.com/FortWorth%252C+TX/
to: http://www.example.com/FortWorth,-TX/
e.g2: http://www.example.com/Fort+Worth%252C+TX/
to: http://www.example.com/Fort-Worth,-TX/
e.g3: http://www.example.com/Fort+Worth/
to: http://www.example.com/Fort+Worth/
(Should not redirect this one)
Note: Please remember your code should not be only for the above URL but for all URLs with the above rule.
Thanks Sumurai8 , for your solution but it needs to be modified like This code :
RewriteEngine On
RewriteRule ^([^+]+)\+(.+)%2C+\+(.*)$ /$1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C+\+(.*)$ /$1,-$2 [R,L]
The first argument of RewriteRule is matched against the %-decoded url, so it should be matched against http://www.example.com/FortWorth%2C+TX/ for example instead of http://www.example.com/FortWorth%252C+TX/.
Assuming the strange substring only occurs once, and the "+" only appears once before that, you can do it with the following two rules. If multiple +'s can occur, things start to turn ugly quickly, because you'll need to either add a lot more rules, or start handling it recursively.
RewriteRule ^([^+]+)\+(.+)%2C\+(.*)$ $1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C\+(.*)$ $1,-$2 [R,L]
I am not sure if mod_rewrite will escape the , character as a "special character". It is a valid character to use in an url. If it gives you problems, you could use the [NE] flag to prevent escaping such characters.

htaccess rewriting all URLs in a path with character replacement

I have a website that was converted from 1 platform to another. I have many thousands of pages indexed that look like this:
domain.com/test/red_widget.html
domain.com/test/big_red_widget.html
domain.com/test/small_nice_red_widget.html
that need to look like this:
domain.com/test/red-widget.html
domain.com/test/big-red-widget.html
domain.com/test/small-nice-red-widget.html
So all the URLs in question in this example are located in the "test" path, use underscores and have an html extension.
And I need all urls in the "test" path with an html extension to be rewritten as is except the underscores, "_", would be replaced with dashes, "-".
I've tried a lot of stuff but I can get compatibility with my current htaccess rules which need to continue to work.
Right now I have this rule:
RewriteRule ^test/(.*).html$ /?kw=$1
which does work but for a url like domain.com/test/small_nice_red_widget.html, the kw parameter will be set as small_nice_red_widget whereas I really need it to be set as small-nice-red-widget.
Any guidance or if anyone can point me in the right directions will be greatly appreciated.
This should work for you:
RewriteRule ^test/(.*)_(.*)_(.*)_(.*).html$ /test/$1-$2-$3-$4.html [R,L]
RewriteRule ^test/(.*)_(.*)_(.*).html$ /test/$1-$2-$3.html [R,L]
RewriteRule ^test/(.*)_(.*).html$ /test/$1-$2.html [R,L]
Place the above, just above your current rewrite rule.
You can also do this to replace an arbitrary number of underscores with slashes:
RewriteRule ^test/(.*)_(.*)\.html$ /test/$1-$2.html [L,E=DASH:Y]
RewriteCond %{ENV:DASH} Y
RewriteRule ^([^_]+)$ /$1 [L,R=301]

Apache URL Rewrite to domain.com/custom_url_name

Using Apache on a Red Hat server, I'm trying to rewrite the URL of a member's store on our website from:
domain.com/store.php?url=12345
to:
domain.com/12345
Using these rules, I can get it to work if I always remember to add an ending slash:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/url/(.*)$ store.php?url=$1
RewriteRule ^(.*)/$ store.php?url=$1
domain.com/12345/ works,
but domain.com/12345 does not work.
Removing the slash in the last line of Rewrite code breaks a lot of stuff. Is there a way to get this to work both with or without that ending slash?
What if you made the slash optional? Furthermore, you probably to to specify something more specific than (.*), because domain.com/a/b/c/d/e will match. Instead, you can use a negated character class to specify everything other than a slash.
RewriteRule ^([^/]*)/?$ store.php?url=$1
Alternately, if you only want to capture numbers, you can use the \d shorthand class (which matches any digit) along with a + which specifies that at least one digit must be present:
RewriteRule ^(\d+)/?$ store.php?url=$1
Your attempt using ^(.*)$ fails because that would match any URL path. Use a more specific pattern than .*, maybe \d+ to allow only one or more digits:
RewriteRule ^(\d+)$ store.php?url=$1