I am migrating my blog from a subdirectory to the root of my domain, but I am leaving the admin panel in its current location, as such I need to do a 301 redirect for anything which matches the following path, except the "admin/" subdirectory.
I have the core redirect working with this:
RedirectMatch 301 (^/oldblogpath/)(.*) http://www.example.com/$2
The path I want to exclude from the above is "/oldblogpath/admin/" - please help me understand what I'm missing!
You can use a negative lookahead pattern for this:
RedirectMatch 301 ^/oldblogpath/(?!admin/)(.*) http://www.example.com/$1
(?!admin/) is a negative lookahead condition that fails the match of admin/ appears right after matching starting directory path.
Make sure to clear old browser cache completely before testing this change.
Related
I have about 200 folders that contain pdfs and docs. They should all be redirected to a subfolder in the same directory. There are also folders and files in the same directory that should not.
The folders all have the same name-structure (123-name-name with - or _ mixed) which match the following with GREP in BBEdit:
(\d{3}(_|-).+)
So I tried something like:
RedirectMatch 302 ^/([\d{3}(_|-).+)]/(.*)/?$ /subfolder/$2
placed in the same folder as the ones I want to redirect.
I have no idea how to set the match correctly.
A URL like example.com/images/123-name-name/somefile.pdf (or doc, docx) should be found at example.com/images/subfolder/123-name-name/somefile.pdf
But the code above results in a Internal Server Error.
It would be great if someone could help me in that.
You could use:
^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$
to capture both parts of the url in capture groups, and replace with $1kunden/$2, so your line will look like:
RedirectMatch 302 ^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$ $1kunden/$2
This would insert kunden/ in between the rest of the url and the file name.
If you want to specify that the filename must have an extension (ie, include at least 1 .) you could replace the regex with:
^(.*?\/\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*?\..*?)$
EDIT My bad, to prevent the recursion, you can use a negative look-ahead to ensure the subfolder doesn't already exist in the path:
RedirectMatch 302 ^((?!.*subfolder\/).*?\/)(\d{3}(?:-|_)\w+(?:-|_)\w+\/)(.*)$ $1subfolder/$2$3
This will prevent a url that already contains subfolder/ from redirecting again.
Hi I want to 301 redirect all urls, including the folder name, within the portfolio folder to the homepage.
There are plenty of similar questions/answers, but the ones I could find focused on simply removing the folder from the URL. I don't want that. I need to redirect to a single URL. i.e. remove everything from the source URL and redirect to the homepage.
example of urls I want to redirect
https://example.com/portfolio/
https://example.com/portfolio
https://example.com/portfolio/this-is-a-page
https://example.com/portfolio/this-is-a-another-page
I've used this regex below, but it doesn't work because instead of redirecting to the homepage, it redirects from:
https://example.com/portfolio/this-is-a-page
to
https://example.com/this-is-a-page
I need it to redirect to just https://example.com/ whatever the /portfolio/ child url. What am I doing wrong?
^/?portfolio/(.*)$
https://example.com/$1
You can use:
^/?portfolio(/|$)
https://example.com/
Without $1 , because this $1 is just there to copy the end of the URL
Rewrite with:
RewriteRule ^/?portfolio(/|$) / [NC,R=301,L]
I'm having a bit of an issue with a 301 redirect to fix some client site requirements.
Here's the scenario:
If I go to website.com/case-studies I would like it to redirect to website.com/case-studies/any/any/any, however, if we have something like website.com/case-studies/technology/any/any it shouldn't redirect.
I have tried Redirect 301 /case-studies /case-studies/any/any/any but this basically put me in a loop of /any * 30 appearing in the address bar.
You will need to a regex based directive and use anchors. So use RedirectMatch instead of Redirect:
RedirectMatch 301 ^/case-studies/?$ /case-studies/any/any/any
Regex ^/case-studies/?$ will match /case-studies or /case-studies/ but it won't match anything beyond that.
Make sure to clear your browser cache when testing this change.
I have urls like www.example.com/de/something and I need to redirect to www.example.com everything that starts with /de/.
At the moment I have done this
redirect 301 /de http://example.com
and it redirect all links but just removing /de part and result is www.example.com/something.
How to fix this?
Thanks
redirect directive matchs rest of the uri and appends it to the target, you can use RedirectMatch to redirect a specific uri
redirectMatch 301 ^/de/? http://example.com
If you want /de/ in the target you should have specified so, becasue Redirect will include in the target everything "after" what you have matched.
For a different virtualhost as the destination you want this:
Redirect 301 /de/ http://example.com/de/
or
Redirect 301 /de http://example.com/de
If what you want is redirect inside the same virtualhost /de to /, then use a negative lookahead.
RedirectMatch ^(?!de) http://example.com/
If the context is .htaccess, for virtualhost It would be:
RedirectMatch ^/(?!de) http://example.com/
Note: I use /de/ originally because that's what you describe in your question, and also I match slashes in the target. Both source and target without slashes would be fine too for cases like /desomething or /de/something. In any case, always match slashes or the lack of them.
Note2: Do not use .htaccess to redirect unless you are not the admin of the site. It just complicates things and adds unnecessary overhead since the file/s need to be checked a number of times per hit.
ATM we are investigating a bug in our webapp which produces duplicated urls for specific content. The duplicated urls get a suffix -X where X is an integer.
The right urls look like this:
http://www.foo.bar/entity/some-special-name
The duplicates look like this:
http://www.foo.bar/entity/some-special-name-1
Till we found the bug, we urgently need a workaround based on mod_rewrite which redirects the duplicate-urls to the originals. Has someone an idea, how I can achieve this kind of redirect? How would a regex look like? The redirect should only fire for urls in the /entity/ subdirectory.
You can use this redirect rule in your site root .htaccess to remove invalid URLs:
RedirectMatch 301 ^/(.+)-\d+/?$ /$1
If you just want to fix /entity/ URLs then use this rule:
RedirectMatch 301 ^(/entity/.+)-\d+/?$ /$1