I have Old URLs in my site and it like this:
http://'www.example.com/newsroom/press-release/item/949-ItemAlias.html
I want to redirect it to :
http://'www.example.com/press/ItemAlias.html
I used this sentence in .htaccess:
RewriteRule ^newsroom/press-release/item/(.*)$ /press/$1 [R=301,NC,L]
But this sentence redirect the URLs to http://'www.example.com/press/949-ItemAlias.html Of course the (949-) is example, I want to remove the number and dash when i redirect the URLs. What is the changes i have to do in .htaccess?
Your regex should match the number, but not include it in the ()'s. ex:
RewriteRule ^newsroom/press-release/item/[0-9]*-(.*)$ /press/$1 [R=301,NC,L]
The [0-9]* matches an arbitrary length number, followed by the -. Only what's matched parens will be included in $1.
Related
How can I redirect a URL that has a "?" in the middle of it (not like a typical query string)?
The problem URL:
https://example.com/michigan/jobs/?/jobs/
I need to rewrite or redirect it to:
https://example.com/michigan/jobs/
I have tried all of these, and none of them work (no change):
1. Redirect 301 https://example.com/michigan/jobs/?/jobs/ https://example.com/michigan/jobs/
2. Redirect 301 /michigan/jobs/?/jobs/ /michigan/jobs/
3. RedirectMatch 302 ^/michigan/jobs/?/jobs/ /michigan/jobs/
4. RewriteRule ^/michigan/jobs/?/jobs/$ /michigan/jobs/ [R=301,L]
RewriteRule ^michigan/jobs/?/jobs /michigan/jobs/
I've tried a bunch of different "generators" but they don't seem to detect the random "?" in the middle of things.
What would be the correct redirect or rewrite rule to handle this?
not like a typical query string
Although that IS a query string, just like any other. Anything after the first ? is a query string, whether that is the intention or not is another matter. So, in your example /jobs/ is the query string.
The Redirect, RedirectMatch and RewriteRule directives all match against the URL-path only, which notably excludes the query string.
To match the query string, you need to use mod_rewrite and an additional RewriteCond directive (a "condition") that compares against the QUERY_STRING server variable, together with a RewriteRule.
For example:
RewriteEngine On
# Removes the query string from "/michigan/jobs/?/jobs/"
RewriteCond %{QUERY_STRING} ^/jobs/$
RewriteRule ^michigan/jobs/$ /$0 [QSD,R=301,L]
The $0 backreference contains the full match from the RewriteRule pattern - basically the full URL-path. This just saves repetition.
The QSD (Query String Discard) flag then discards the original query string from the redirect response, otherwise it is passed through by default.
Note that the URL-path matched by the RewriteRule pattern does not start with a slash.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
I want to redirect /event/path1/{id}/{some_name} to /event/path2/{id},
I want to redirect old page to the new one and remove the last segment of the url,
I have done with this but not removing the last segment of the url, this is what .htaccess
RewriteRule ^event/path1/(.*) /event/path2/$1 [L,R=301]
You're using .* in your regex that matches everything after /event/path1/ thus resulting in wrong target URL.
You may use:
RewriteRule ^event/path1/([^/]+)/[^/]+/?$ /event/path2/$1? [L,R=301,NC]
Here [^/]+ will match 1 or more of any non-slash character.
? at the end of the target will strip off any previous query string.
I need your help regarding redirect rules.
I have an old URL structure:
www.example.com/en/news/news/single/article/SPECIFIC-ARTICLE/
(with SPECIFIC-ARTICLE being a placeholder for a whole bunch of articles - with a trailing slash!).
After the relaunch the name and structure of the folders will change to:
www.example.com/en/news/SPECIFIC-ARTICLE
So all articles (with trailing) in the old structure should be redirected to the corresponding articles in the new structure. And shouldn't have a trailing slash anymore.
How should the mod_rewrite rule in .htaccess look like?
And how would this rule change if there wasn't a trailing slash but a .html at the end of the old article URLs?
Using mod_rewrite in .htaccess this would be something like (near the top of the file):
RewriteEngine On
RewriteRule ^(en/news)/news/single/article/([\w-]+)/$ /$1/$2 [R=302,L]
$1 is a backreference to the en/news match in the RewriteRule pattern (this is simply to avoid repetition). $2 is a backreference to the SPECIFIC-ARTICLE (which is assumed to consist of just the letters A-Z, a-z, 0-9, _ and -).
If there is .html instead of the trailing slash, then change the RewriteRule pattern to:
RewriteRule ^(en/news)/news/single/article/([\w-]+)\.html$ /$1/$2 [R=302,L]
Note the dot is backslash escaped to match a literal dot and not any character.
Note that this is a temporary (302) redirect, change this to a 301 (permanent) redirect only when you are sure it is working OK.
I want a redirection in my website.
The old url looks like this :
http://www.example.com/Prem_Ratan_Dhan_Payo/Prem_Leela.html
Now we have to 301 redirect it to
http://example.com/songs/prem-leela/
In total I have to
Check if the url end with .html then we will redirect it other wise let it pass.
remove .html from end.
make all letters in lower case.
change underscore to hyphen.
change second segment to 'songs'.
Please help me.
This should get you started:
RewriteEngine On
# Replace underscores with hyphens in all requests ending in .html.
# The rule replaces one char at a time and is repeated until no matches are found.
RewriteCond "%{QUERY_STRING}" "\.html$" [NC]
RewriteRule ^([^_]*)_(.*) $1-$2 [N]
# Capture the file name without extension inside braces
# Use a lower-case mapping on the file name.
# Destination is /songs/filename
RewriteMap lc int:tolower
RewriteRule ^.*/(.*)\.html$ /songs/${lc:$1} [NC, R=301]
I want to apply RewriteRule for specific pages in my website.
Original link :
www.abc.com/category/foo.html
How will I apply RewriteRule to redirect to homepage (www.abc.com) if any link contains any character (not number) after the slash (category/_123etcetc) "www.abc.com/category/" ?
For example;
i want to redirect www.abc.com/category/apple.html or www.abc.com/category/key.html to homepage but i don't want to redirect www.abc.com/category/1-apple.html or www.abc.com/category/123-key.html