I need a htaccess rewrite rule to rewrite:
https://example.com/accounts/{user}/info
to
https://example.com/accounts/info
The {user} will be a dynamic part where this directory is not available so it needs to be a wildcard part within the rule.
Since it will be a URL where query posts will be sent to I need to forward all query params.
To internally rewrite the request (ie. the URL does not change in the browser's address bar), you can do something like the following in the .htaccess file in the document root:
RewriteEngine On
RewriteRule ^accounts/[^/]+/info$ accounts/info [L]
The "wildcard" regex [^/]+ matches 1 or more characters except the slash (so it doesn't eat into the next path segment). If {user} should match a specific pattern then the regex should be made more specific.
Any query string parameters that are on the original request are passed through to the target URL.
Related
I've many links of this type:
https://example.com/?redirectTo=G04BIQ8LGG&redirect_prodid=xyz-G04BIQ8LGG
I need to redirect to amazon affiliate link:
https://www.amazon.com/dp/G04BIQ8LGG?tag=mytag-21&linkCode=ogi&th=1&psc=1
The only part to take from old url is the product code (ex.G04BIQ8LGG)
Someone can help me with .htaccess rule and regex?
Thanks!
unfortunately no, I'm not very good with regex.
The regex is very similar as in the linked question. However, the required mod_rewrite directives themselves are much simpler in this case.
For example:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^redirectTo=([^&]+)
RewriteRule ^$ https://www.amazon.com/dp/%1?tag=mytag-21&linkCode=ogi&th=1&psc=1 [NE,R=302,L]
I've anchored the redirectTo URL parameter to the start of the query string, since that is how it appears in your example. In the linked question, the URL parameter can appear anywhere in the query string since that would seem to have been a requirement in that question.
Since the URL parameter value is used in the URL-path of the redirected URL, the NE (noescape) flag is required to prevent a %-encoded URL param value being doubly encoded in the resulting redirect. (Although this is not an issue if this URL param value is never %-encoded - it doesn't necessarily look as if it would be.)
I have changed the path of some of my urls, and I'd like to redirect them to some new urls with an extra query string ?industry=
Old url: e.g. https://domain.ext/skills/keywords/list.php?q=account+manager
https://domain.ext/skills/keywords/list.php?q=assistant
New url: e.g.
https://domain.ext/skills/keywords/list.php?industry=human-resources&q=account+manager
https://domain.ext/skills/keywords/list.php?industry=banking&q=account+manager
https://domain.ext/skills/keywords/list.php?industry=banking&q=assistant
As you can see the new URls contains a new query parameter industry= which can have different industries.
So how can I check if a new url exist with the query ?q=account+manager for example and then redirect the old url to one of the new url that match &q=account+manager?
I've looked into RewriteRule with .htaccess, but I haven't been able to find the right redirection yet. If someone could share some thoughts. Thanks.
Not sure what you wish to do, but I will take this question as a request for a simple example of a Rewrite Rule. Depending on what you are trying to achieve, several approaches may be used.
The code in the .htaccess file could be like this:
# essential
Options +FollowSymLinks
RewriteEngine On
# Here a simple rule, the link ending with assitant will be sent to list.php as the q parameter
RewriteRule ^skills/keywords/assistant$ list.php?q=assistant
# You may use variables and Regex, such as $1, $2, $3 in this case to receive the values
# in the parentheses for the three parameters
RewriteRule ^(en/fr/es)/(keywords)/([\/\.\w_-]{3,})$ list.php?lang=$1§ion=$2&q=$3 [L,R=301,NC]
Flags are these ones:
NC = No Case
R=301 = returns a 301 (permanent redirection) code rather than a
302 (temporary) for SEO purposes
L = Last ruel to be executed,
next ones are ignored.
I haven't check if this code works, when testing this on your server, you will encounter a blank page if something is wrong. Also, if you use flags, make sure there is no whitespace after the comma.
This page explains all this very well: https://httpd.apache.org/docs/trunk/en/rewrite/intro.html
Situation: after a bunch of products on an online store were renamed, the URLs needed to be changed to match the new names. Redirects were set up to handle the old vs new URL conversion.
Since the old URL's could include these model names in several various places, a "keyword" approach was taken. In other words, "if THIS_STRING occurs somewhere in the requested URL, regardless of path, redirect to this new URL".
Some redirects work, since the old model name and the new model name is different:
RewriteRule ^.*(CC88SPECIAL).*$ http://www.example.com/widgets-DD99EXTREME [L,NC,R=301]
This would properly redirect any request containing "CC88SPECIAL" to the new URL.
Problem is, the same syntax could not be applied to models where the new name included the old name, i.e. AA55COMPLETE became AA55COMPLETE100:
RewriteRule ^.*(AA55COMPLETE).*$ http://www.example.com/widgets-AA55COMPLETE100 [L,NC,R=301]
The asterisk after the "keyword" would produce a loop.
So the format was changed to:
RewriteRule ^.*(AA55COMPLETE).$ http://www.example.com/widgets-AA55COMPLETE100 [L,NC,R=301]
Problem is, this has no effect. No loop, no error, no 503, just doesn't change the URL at all.
What am I doing wrong, and what's the correct syntax to make it right? I.e. match a specific string inside the requested URL, in any position?
Your last rule failed because it does not match the requested uri /AA55COMPLETE because of the trailing . in your regex pattern. Dot in regex means to match one more character so its trying to match /AA55COMPLETEA instead . To redirect /AA55COMPLETE to /AA55COMPLETE100 you can use
RewriteEngine on
RewriteRule ^AA55COMPLETE/?$ /AA55COMPLETE100 [NC,L,R]
I am designing a News Website using joomla 2.5
I want rewrite this url:
http://domain.com/categoryname/?format=feed&type=rss
to:
http://domain.com/rss/categoryname
Note: I'm using mode_rewrite .htaccess for joomla.
please help me quickly.
thanks to every body in this site.
Apache's mod_rewrite allows you to transform a url to a different url utilizing regex patterns.
The pattern applies to the path and allows you to do your in your example write a regex pattern like /rss/(.+) which will match anything beginning with /rss/ and has at least one character after. The parenthesis are called a capturing group and you can reference that in the second parameter in the RewriteRule directive.
The second part /$1/?format=feed&type=rss, references the first captured group in the pattern and places it in the new url.
Finally you want to signify that it is the last rule to be processed with an [L] flag.
This gives you a rule of:
RewriteEngine On
RewriteRule /rss/(.+) /$1/?format=feed&type=rss [L]
If you intend to pass query strings to this new url, you will need to add an additional flag QSA which will result in [L,QSA] in place of [L].
Lets say I want users to be able to type this url in:
www.website.com/blog/2453/I-gained-0.1%-more-scripting-knowledge-!
I'm trying to include title information in the url for seo benefits.
I also want to include an id for my query. Effectively I want to pick up the id and ignore the title stuff that comes after, bearing in mind its user generated text so could contain any special characters in it.
How can I write a .htaccess rewrite rule so that the server reads it as the following with the appropriate GET data:
www.website.com/blog.php?id=2453
This is what I have tried but frankly I am way out of my depth here:
RewriteRule ^blog/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ blog.php?id=$1 [NC,L]
The rewrite rule you are using should work except for the ., %, and ! characters that are in your URL. The % characters is not safe to use in URLs because it has a special meaning in the URL syntax. I wouldn't use exclamation points either.
If the ID is always going to be numeric, use ([0-9]+) instead of ([A-Za-z0-9-]+).
Try this URL:
www.website.com/blog/2453/I-gained-0.1-more-scripting-knowledge
With this rule:
RewriteRule ^blog/([0-9]+)/[A-Za-z0-9\-\.]+/?$ blog.php?id=$1 [NC,L]