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]
Related
I want to redirect
https://example.com/product-info/A100001
to
https://example.com/product-info/index/index/id/A100001
using htaccess redirect rule
A100001 will be dynamic like
A100001
A100002
A100003
A100004
....
I am trying this
RewriteEngine on
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Source
Also tried other example but not working in my scnario
Anyone who expert in htacees rules can help me in this.
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Your example URL contains a URL-path only, it does not contain a query string. The rule you've posted would redirect /?product-info/A100001 to /routing/index/index/id/.
Try something like the following instead:
RewriteRule ^(product-info)/(A\d{6})$ /$1/index/index/id/$2 [R=302,L]
The above would redirect a request of the form /product-info/A123456 to /product-info/index/index/id/A123456.
The $1 backreference simply contains product-info, captured from the RewriteRule pattern (saves repitition) and $2 contains the dynamic part (an A followed by 6 digits).
This is a 302 (temporary) redirect. Always test first with a 302 to avoid potential caching issues.
The order of directives in your .htaccess file is important. This rule will likely need to go near the top of the file, before any existing rewrites.
UPDATE:
redirection is working with your code, Can you please let me know the parameter pattern, I need the number from A452218 to A572217
Regex does not handle numeric ranges, only character ranges. If you specifically only want to match numbers in the stated range then you would need to do something (more complex) like this:
RewriteRule ^(product-info)/A(45221[89]|4522[2-9]\d|452[3-9]\d{2}|45[3-9]\d{3}|4[6-9]\d{4}|5[0-6]\d{4}|57[01]\d{3}|572[01]\d{2}|57220\d|57221[0-7])$ /$1/index/index/id/A$2 [R=302,L]
NB: The $2 backreference now only contains the dynamic number, less the A prefix, which is now explicitly included in the substitution string.
I want to have personalized urls, where a person receives a mailer, that has their purl on it, and redirect them to a landing page that receives their name via query string
The url would be like
mywebsite.com/phx/joe.smith
I would like to redirect this traffic to
mywebsite.com/youngstown/index.php?first=joe&last=smith
This should be a 301 redirect, case insensitive. I'm able to do it if the directory was /phx/firstname/lastnaname, but I need it with the dot between first and last rather than a directory.
What I have so far is
RewriteEngine on
RewriteBase /
RewriteRule ^phx\/([^\/]+)\/([^\/]+)\/? youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx\/([^\/]+)\/? youngstown/index.php?FirstName=$1 [NC,R=301,L]
Any help would be much appreciated!
First, you don't need to escape slashes /. Apart from that, you're almost there. The main difference is \. vs /, e.g.
RewriteRule ^phx/(.+?)\.(.+)/?$ youngstown/index.php?first=$1&last=$2 [R,NC,L]
A complete solution could be:
RewriteEngine on
RewriteRule ^phx/([^./]+)\.([^./]+)/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx/([^./]+)/?$ /youngstown/index.php?FirstName=$1 [NC,R=301,L]
As Olaf says, no need to escape slashes. I am matching here on "not a dot or slash". Also adding $ to delimit the end of the match and removing RewriteBase which is not needed.
Alternative Solution
Alternatively you could use a single rule which would set an empty param for LastName when not present:
RewriteEngine on
RewriteRule ^phx/([^./]+)(?:\.([^./]+))?/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
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.
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.
The following mod_rewrite rule accomplishes the task of rewriting www.domain.com/2011/11/page to www.domain.com/page but breaks www.domain.com/2011/11/ (i.e. breaks WordPress Archive listings) and redirects it to the root of the site.
The rewrite rule should only rewrite items that have content after ^([0-9]{4})/([0-9]{1,2})/page but not ^([0-9]{4})/([0-9]{1,2})/.
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]
Any recommendations?
ANSWER
The initial forward slash was missing at the beginning:
RewriteRule ^/([0-9]{4})/([0-9]{2})/(.*)$ /$3 [NC,R=301,L]
and the WordPress permalink entry needed:
/%postname%/
instead of:
%postname%
although I am not sure how much the latter helped.
I think you need to change the * to a +
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]
should be
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
With (.), it could possibly match nothing, thus a request like "/2011/11/" will match but the back reference for (.) will be blank, thus the rewrite goes to "/". The + indicates that there needs to be at least 1 character satisfying the "." in the regular expression.