Htaccess 301 change old URL - apache

I would like change url in my htaccess and using redirect 301 for SEO.
my old url:
RewriteRule ^my-oldurl-(([0-9]+)-[a-zA-z0-9_-]+).html$ index.php?page=my-detail&id=$1&cat=$2 [QSA,L]
my new url:
RewriteRule ^my-newurl-(([0-9]+)-[a-zA-z0-9_-]+).html$ index.php?page=my-detail&id=$1&cat=$2 [QSA,L]
My last test (error 500):
RewriteRule ^my-oldurl-(([0-9]+)-[a-zA-z0-9_-]+).html ^my-newurl-(([0-9]+)-[a-zA-z0-9_-]+).html [QSA,R=301]
Thank you for any advice.

Your question is a bit fuzzy, but I assume this is what you are looking for:
RewriteRule ^my-oldurl-([0-9]+)-([a-zA-z0-9_-]+)\.html$ index.php?page=my-detail&id=$1&cat=$2 [QSA,L,R=301]
Or, if you prefer a 2 step approach you can do this:
RewriteRule ^my-oldurl-([0-9]+)-([a-zA-z0-9_-]+)\.html$ my-newurl-$1-$2.html [QSA,L,R=301]
and rely on the already existing rewrite rule for the new urls.
Also note the backslash escaping (\) I added to the dot (.) in the regular expression. It works without, but this version is more precise.

Related

301 Rewrite Rule Not working

We want to redirect Below mentioned URL.
From URL:
www.example.com/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0
To URL : www.example.com/4G
We wrote 301 rules in apache configuration as below.
RewriteEngine on
RewriteRule ^/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0 /4G [L,R=301]
But redirection is not working as expected. Any Suggestion will be highly appreciated.
mod_rewrite will not look at query strings as part of its matching, unless you specifically ask it to:
RewriteCond %{REQUEST_URI} ^/itemLevelFilterPage\.action$
RewriteCond %{QUERY_STRING} ^keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0$
RewriteRule (.*) http://www.example.com/4G? [R=301,L]
The first condition makes sure we're looking at the right page, the second checks the query string, and the rule will rewrite the URL to the desired one.
The trailing question mark on the rule ensures the old query string is removed. If you're on Apache 2.4 the query string discard flag is available:
RewriteRule (.*) http://www.example.com/4G [R=301,L,QSD]
More examples here.

Redirect directory with period to query strings in htaccess

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]

Opencart 301 Redirects

Having a problem with redirect in a .htaccess file on an Opencart store.
It seems that any URL with /index.php?_route_= isn't getting redirected.
For example, this works:
redirect /old-url-here http://example.com/new-url?
This doesn't:
redirect /index.php?_route_=some-url.asp http://example.com
Any idea or suggestions as to what might get this to work?
You can't match against the query string using mod_alias' Redirect directive. You'll need to use mod_rewrite, and if you use mod_rewrite, you're probably going to want to stop using mod_alias altogether.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} route=some-url\.asp
RewriteRule ^index\.php$ http://example.com/
Another thing is - apart from Jon's answer - that URLs like index.php?_route_=some-keyword are used/created only internally and only in case you have the SEO turned on. In this case, you click on a link with URL like http://yourstore.com/some-keyword and this URL is rewritten into index.php?_route_=some-keyword.
Therefore you shouldn't be creating URLs like that on your own nor try to redirect from them. If you need to redirect, catch the first SEO URL to redirect it.
We do not know where did you put your changes into the .htaccess file, but if you take a close look at this rewrite rule
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
not only you'll find out it is the last rule there, but it also has this L switch which is telling Apache server that if this rule is matched, do a URL rewrite(, attach the query string [QSA] and) stop matching other rules [L] - this is the Last one.
If you need to redirect only a specific URL, do it the same way as redirect for sitemap.xml is done (for example) and place it before the last rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
# ...
# your new rule here - while the URL in the link is http://yourstore.com/some-url.asp
RewriteRule ^some-url.aspx$ index.php?route=some-folder/some-controller [L]
# ...
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

Simple RewriteRule for first directory

This is pretty basic but I can't find a solution that works. I just need to redirect any URLs from an old directory to a new URL.
Example:
/search/whatever
to
/jobs/search
I don't need to map the whatever, I want all traffic to a URL that begins /search to get redirected (301).
Using this:
RewriteRule /search /jobs [R=301,L]
Works but I have a URL within /jobs that also now gets redirected:
/jobs/search
And that's wrong - it needs to match the start of the URL. So I tried this:
RewriteRule ^/search /jobs [R=301,L]
But that doesn't redirect at all, so I'm stuck.
Another example would be this:
RewriteRule /careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
This creates a loop as careers-at-pure is in the old and new URLs, but the following doesn't get matched and redirected:
RewriteRule ^/careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
Any suggestions?
Thanks
The leading slash is removed from the URI when they're being put through rewrite rules in htaccess files. You need to remove the leading slash from the regex, or at least make it optional:
RewriteRule ^/?search /jobs [R=301,L]

How can I apply an htaccess rewrite rule to a URL containing a linefeed character (%0A)?

I'm running into bad URLs that contain an escaped line-feed in the following format:
http://domain.com/%0Apath/to/file.txt
However, even if I try the most global rewrite possible...
RewriteRule ^.*$ /path/to/file.txt [R=301,L]
...Apache still throws a 404:
The requested URL / path/to/file.txt was not found on this server.
(Note the space.)
How can I gracefully intercept these bad URLs and route them to the right destination?
I had the same Problem. I fixed it with the help of sarumont's answer.
Example URL, found in Webmaster Tools:
/dummy-url/dummy-%0A%0Afull-version-download
Rewrite Rule that I added in Apache config:
RewriteRule ^/dummy-url/dummy-[\n\r]+full-version-download$ /dummy-url/dummy-full-version-download [L,R=301]
^.*$ won't span the linefeed. Try a plain .*. Alternatively, try matching on the newline characters: [\r\n].
Adding \s to the RewriteRule should fix it.
RewriteRule ^\s.*$ /path/to/file.txt [R=301,L]
More specifically as a catch-all
RewriteRule ^\s(.*)$ http://www.example.com/$1 [R=301,L]
This is very old question but I think none of the answer are right so posting an answer:
Replace your rule with this:
RewriteRule .* /path/to/file.txt [R=301,L]