Simple RewriteRule for first directory - apache

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]

Related

Rewrite Rule which takes wildcard characters and add it to the destination page

I need a rewrite rule which redirects to a subdomain, but takes takes everything from wildcard to the destination page
for example:
if
https://example.org/product/abcde
is called, the redirect should go to
https://new.example.org/product/abcde
What I have already have is:
RewriteRule ^/product/(.*)$ https://new.example.org/product [R=301,L]
but this Rule "forgets" everything after /product/ in the redirect
Is it possible add the wildcard characters to the new URL?
What I tried is:
RewriteRule ^/product/(.*)$ https://new.example.org/product/(.*) [R=301,L]
But then the redirect goes to to the URL
https://new.example.org/product/(.*)
instead of
https://new.example.org/product/abcde
I could find a solution, which works perfect for my requirement
RewriteRule ^/produkt(.*)(.*) https://new.example.org/produkt$1/$2 [R=301,L]
Solutions was found at https://alvinalexander.com/web/apache-redirectmatch-examples-wildcard-301

How to rewrite url and redirect with apache mod rewrite?

I have got url:
ipaddress/panelname/main/index.php
How to rebuild it to
ipaddress/center/index.php
?
ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.
I tryed to do this in .htaccess:
RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1
but i don't see redirect from panelname to center.
but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)
How to rewrite all to another links and howto see redirect from old links to my new? Thank you.
RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.
Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).
You either have to do the following:
RewriteRule ^panelname(.*)$ /center$1 [R,L]
or:
RedirectMatch 302 ^/panelname(.*)$ /center$1
Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.
If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).
RewriteEngine on
# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]
# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [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]

Apache - rewrite rule for user folders

I'm trying to get a url like www.domain.com/user/myuser to forward to www.domain.com/user/index.html?user=myuser the code below does not work.
RewriteRule ^user/?$ /user/index.html?user=$1 [QSA,L]
does not work. is not helpful.
In this case your regex seems to be wrong. ^user/?$ means starts with user has an optional slash as the last letter
what you mean is
^user/(.*)$ which means match user/anything and redirect to /user/index.html?user=anything
so
RewriteRule ^user/(.*)$ /user/index.html?user=$1 [QSA,L]
should work

Mod Rewrite -- redirect all content from subdirectory

I have a scenario where there is a a site with subdirectories and content etc originally in a subdirectory /main
The site and all content has been moved back to the root and is working fine
We need to rewrite so that any http call to /main/, /main/page1, /main/page2 etc is redirected back to the / directory but the uri /page1, /page2 etc
This is what we have so far
RewriteCond %{REQUEST_URI} ^/main/.*
RewriteRule ^/main/(.*) /$1 [L]
Any comments welcome
Thanks very much
In .htaccess context, the url that is matched in the first parameter of RewriteRule doesn't include a leading slash and doesn't include the query string. Having a leading slash will cause the rule to never match. In your case your RewriteCond is unnecessary, as it matches exactly what the RewriteRule would match. Change your rule to the following url and it should work. Please note that this is an internal rewrite (the client won't see this change). If you need a redirect (the client will display the url without main in the address bar), add the [R] flag to the rule.
RewriteRule ^main/(.*)$ $1 [L]
See the documentation.