I have an URL:
example.com/fruits12345/mango
I want to redirect it to
example.com/fruits/12345/mango
*any number of digits after fruits should be redirected with fruits/NUMBERS
Tried But failed as it creates too many redirects:
RewriteEngine on
RewriteBase /
RewriteRule ^fruits(.*)/(.*) /fruits/$1/$2 [R=301,L]
You may use this rule:
RewriteEngine on
RewriteRule ^(fruits)(\d+)/(.*) /$1/$2/$3 [R=301,L,NC,NE]
Make sure to clear your browser cache before testing this rule.
Problem with your rule is that you are matching .* after fruits which matches anything so a redirected URL /fruits/12345/mango also matches this pattern and gets redirected again and again causing redirect loop.
Related
I am trying to redirect everything under https://example.com/work/ to https://example.com/work/john/.
This is what I've done:
RewriteEngine On
RewriteBase /
RewriteRule ^work/(.*)$ work/john/$1 [L,R=301]
While testing, the url: https://example.com/work/design/download
gets redirected to:
https://example.com/work/john/john/john/john/john/john/john/john/john/john/john/john/john/john/john/john/john/john/john/john/john/design/download.
Chrome gives me the error: ERR_TOO_MANY_REDIRECTS.
How do I fix this?
You may use this rule with a negative lookahead:
RewriteEngine On
RewriteBase /
RewriteRule ^work/(?!(?:sam|john)(?:/|$))(.*)$ work/john/$1 [L,R=301,NE,NC]
(?!john(?:/|$)) is a negative lookahead that skips redirect when john/ or john comes right after /work/ in URI.
Make sure to completely clear your browser cache before testing this change.
I am trying to redirect these pages
/reviews/page/2/
/reviews/page/3/
to this
/reviews/
using this line:
Redirect 301 /reviews/page/./ /reviews/
But it's not working. I've tried other combinations like .* and ^.*$ but nothing works. Only a specific URL will get redirected to the new one.
Is there anything else that could interfere with the line I'm trying to work? Maybe space, uppercase, lower case, indent, etc?
The whole file is pasted below.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# --------------------------------
# | ADDITIONAL RULES |
# --------------------------------
<FilesMatch "^robots\.txt">
Order Allow,Deny
Allow from all
</FilesMatch>
<FilesMatch "^\.htaccess|.*\.cgi">
Order Deny,Allow
Deny from all
</FilesMatch>
Redirect 301 /reviews/page/./ /reviews/
The mod_alias Redirect directive is prefix-matching, it does not accept wildcards or regex. So, you could theoretically do something like:
Redirect 301 /reviews/page/ /reviews/
However, as mentioned, the Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL. So, a request for /reviews/page/2/ would be redirected to /reviews/2/ - which is not desirable.
You could use RedirectMatch instead, which uses regex rather than simple prefix matching. However, since you are already using mod_rewrite (RewriteRule) directives, it is preferable to use mod_rewrite for this in order to avoid potential conflicts. Different Apache modules execute at different times during the request, regardless of the apparent order of these directives in the config file.
Instead, try the following mod_rewrite directive at the top of your .htaccess file, immediately after the RewriteEngine directive:
RewriteRule ^reviews/page/[23]/$ /reviews/ [R=302,L]
This matches any request for /reviews/page/2/ or /reviews/page/3/ and redirects to /reviews/. The 2 or 3 are matched using a character class. Note that in per-directory .htaccess files the URL-path that the RewriteRule pattern matches against does not start with a slash.
This is also a 302 (temporary) redirect. Change this to 301 (permanent) redirect - if that is the intention - but only after you have tested that it's working OK. 301s are cached hard by the browser, so can make testing problematic.
You'll need to clear your browser cache before testing.
UPDATE: To match any digit you can change [23] to \d (the shorthand character class for digits. To match 1 or 2 digits, you can use \d{1,2}. For example:
RewriteRule ^reviews/page/\d{1,2}/$ /reviews/ [R=302,L]
You could use . (dot) to match any character. However, this might be too broad for what looks like a page number. Regular expressions (regex) should be as restrictive as possible.
In .htaccess I want to redirect urls like this:
/products.php/a7-frames
/products.php/a6-frames
to this:
/picture-frames/a7-frames
/picture-frames/a6-frames
So need to substitute products.php with picture-frames.
After a lot of Googling I tried this:
RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1
But it doesnt work, if I enter this url: /products.php/a7-frames the browser says there are too many redirects and goes to:
/picture-frames/index
It's substituting the products.php for picture-frames which is great, but I'm not sure why it adds "index" on the end rather than the /a7-frames part of the url? How can I fix this?
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]
Make sure to clear your browser cache before testing this change.
I'm rewritting this
http://myweb.com/post/5/title-of-the-post (new url)
To
http://myweb.com/post.php?id=5 (old url)
I have achieved this by:
RewriteEngine On
#Options +FollowSymLinks
RewriteRule ^post/(.+)/(.+) post.php?id=$2
But if I try to enter to the old url it's still possible, so how can I redirect to the new one and not end up in a loop?
First, add this rule:
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^post\.php$ post/%1/article [R=301,L]
This will force an immediate redirection and will not process any further rules. Requests to post/what/ever won't match this rule and skip on by. Note you have to create a generic article title, as .htaccess has no way of knowing the title to use.
Next, add this rule:
RewriteRule ^post/([0-9]+)/(.*)$ post.php?id=$1 [L]
Requests to post.php will have already been caught above, so they won't reach this line. This will do the redirection as expected and you should not have the endless loop.
I am trying to make a sitewide 301 redirect for a site with around 400 pages but also have a subset of about 10 individual pages that don't follow the sitewide redirect and should point somewhere else.
Any ideas how to format such redirect rules so the sitewide redirect doesnt conflict with the subset pages redirect?
I am starting with the sitewide redirect rule as:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.name.com/$1 [R=301,L]
The rewrite rules are parsed in the order they are written, so the order you list them also defines the priority.
Given that, you should first match the request URI with the 10 individual pages and return the redirection accordingly, and then define the sitewide redirection.
If the 10 individual pages have a single target URL, the match rule may be one, otherwise you should do a single redirection per each request URI.
Take care to use the [L] flag for the first redirections, to tell the server to exit the routine if the rule is matched, and I would also suggest to add the line
RewriteBase /
which is pivotal for some Apache versions, in which the omission of this line may cause a http bad conf error.
Options +FollowSymLinks
#switch on the rewrite engine:
RewriteEngine On
RewriteBase /
#rules for the individual redirections:
RewriteRule http://example.com/myUrl-1 http://www.example.org/new-1 [R=301,L]
RewriteRule http://example.com/myUrl-4 http://www.example.org/new-2 [R=301,L]
RewriteRule http://example.com/myUrl-3 http://www.example.org/new-3 [R=301,L]
#...and so on
#sitewide redirection rule:
RewriteRule (.*) http://www.example.org/$1 [R=301]