I have a similar situation to the one described in the title.
All that I need to do is map all requests in the form /search/?q=test to /test. This is because we are changing the way our search works to make it user friendly, but still want to allow for backward compatability (i.e. anyone that may have these links bookmarked etc).
However, thus far I have this:
RedirectMatch 301 /search/?q=(.*) /$1
And that doesn't work, but:
RedirectMatch 301 /search/(.*) /$1
does...
Any idea why?
Cheers.
have you tried with RedirectMatch 301 /search/\?q=(.*) /$1 ?
I am using the \ before the ? (to force it being used as a literal character) in case it gets treated as a special char (which it is, in regular expressions..)
Related
I am trying to redirect a subfolder as well as anything after it to the home page.
For example:
example.com/subfolder/extra-stuff > example.com
The extra-stuff is constantly changing and auto generated, so I want the redirect to remove that as well.
I am using:
Redirect 301 /subfolder(.*) http://www.example.com
However, this will result in http://www.example.com/extra-stuff.
Is there a way I can say if /subfolder(and anything else after subfolder) redirect to home?
Thanks for any suggestions!
The Redirect directive uses simple prefix-matching and everything after the match is copied onto the end of the target URL (which is what you are seeing here). However, the Redirect directive also does not support regex syntax, so a "pattern" like (.*) on the end will actually match the literal characters (, ., * and ) - which shouldn't have worked in your example?!
You'll need to use RedirectMatch instead (also part of mod_alias), which does use regex, and is not prefix matching.
For example:
RedirectMatch 301 ^/subfolder http://www.example.com/
Any request that starts /subfolder will be redirected to http://www.example.com/ exactly.
You'll need to clear your browser catch before testing.
You tagged your question "Magento" (which is probably using mod_rewrite). You should note, however, if you are already using mod_rewrite for rewrites/redirects then you should probably be using mod_rewrite instead of mod_alias to do this redirect, since you can potentially get conflicts.
For example, the equivalent mod_rewrite directive would be:
RewriteRule ^subfolder http://www.example.com/ [R=301,L]
Note there is no slash prefix on the RewriteRule pattern. This would need to go near the top of your .htaccess file.
Running into this issue where the redirect in the second line is redirecting to the URL in the first line.
Redirect 301 /academics/degrees http://mydomain.edu/folder1/location1/
Redirect 301 /academics/degrees/phd http://mydomain.edu/folder1/location2/
At first I thought it had something to do with the locations to be redirected containing hyphens, but haven't been able to find anything on that.
Does it have something to do with the locations to be redirected sharing the same folder/permalink structure?
I've never encountered this before and am totally lost. I tried RedirectMatch but that didn't have any effect.
It is because /academics/degrees matches both URLs and rule for /academics/degrees/phd never fires. Either change the order of your rule OR better use RedirectMatch with regex capability to match only desired URL pattern:
RedirectMatch 301 ^/academics/degrees/?$ http://mydomain.edu/folder1/location1/
RedirectMatch 301 ^/academics/degrees/phd/?$ http://mydomain.edu/folder1/location2/
Make sure to clear your browser cache before testing this change.
i have setup up a redirect
RedirectMatch 301 /data(.*) http://www.site.com/sites/default/files/datassheets$1
and i am getting the following error
http://www.site.com/sites/default/files/datasheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheets/doc3542.pdf
when i rename the datasheets directory to something else it works but this is not an option
is this an apache error or am i doing something wrong
Your RedirectMatch regular expression /data(.*) is matching on every request and thus will continue indefinitely.
What the complete redirect rule will look like depends on your use-case. The following rule takes care of the endless loop issue and redirects the content following /data/ to the new structure at http://www.site.com/sites/default/files/datasheets/.
RedirectMatch 301 ^/data/(.+) http://www.site.com/sites/default/files/datassheets/$1
/data/my-cool-file =>
http://www.site.com/sites/default/files/datassheets/my-cool-file
the (.*) portion that you have after /data is matching sheets in your url. You are then taking that match and appending it on the redirect. That's what's giving you the repeating word. I'm guessing you're also redirecting to your own site, which is why it's repeating so many times.
What are you expecting to come after data that you want to append to the redirect? If it's a query string, you can add [QSA] as a flag at the end to maintain the query string.
Example:
RedirectMatch 301 /data/(.*) http://www.site.com/sites/default/files/datassheets/$1 [QSA]
Also, consider that you are telling everyone that any page that starts with data in any directory shouldn't exist, yet you are redirecting them to a page that matches the very same pattern you are supposedly getting rid of. You probably need to expand the regex to only match what you intend.
I got some problem on redirecting something.
I tried to use Redirect 301 /link/link/link to /link/link
Is there a way to make it more easier coz there are 100+ links I need to redirect.
Like this
/blog/category/energy-savings/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
To the Homepage?
Thanks
The directive:
Redirect 301 /link/link/link /link/link
Doesn't do what you think it does. It's mapping to nodes together, meaning a request for:
/link/link/link/foo/bar.html
gets redirected to:
/link/link/foo/bar.html
So maybe you need to fix that. You could try:
RedirectMatch 301 ^/link/link/link/?$ /link/link
so the nodes aren't connected like in the Redirect directive. As for trying to "fix" this problem of yours, you can try:
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/\4/ /
This redirects anything that looks like: /blah1/blah2/blah3/same/same/same/ etc. to the homepage at /. The \4 matches the 4th path in the URI, so if anything after the 3rd path repeats, then it redirects.
I would like to redirect any php page with optional parameter to a new clean url
eg, from: account.php?id=156 to newurl/account/156
I'm using the following redirectmatch
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)[\.php]?[\?]?[a-zA-Z_]?[=]?([0-9]+)?$ /cmstut/redirect/newurl/$1/$2/$3 [L]
but the result I get is it will redirect to newurl/account//?id=156
I thought it was funny when I read somewhere where htaccess and regular expression was compared to voodoo :) we'll now I understand why
I don't understand where your third subexpression is.
$1: ([a-zA-Z0-9_]+)
$2: ([0-9]+)
$3: MIA
May I recommend something more like this?
/cmstut/([A-Za-z0-9]+)\.php(\??id=([0-9]+))?
then you may have to use $3 to access the id number. you want the entire parameter to be optional, right?
I never used URL redirects in .htaccess before, but if it's plain regular expressions, this should work:
RedirectMatch ^/cmstut/([a-zA-Z0-9_]+)\.php(\?[a-zA-Z_]+=([0-9]+))?$ /cmstut/redirect/newurl/$1/$3 [L]
Since you weren't very specific in what you want, I took some guesses. This will redirect foo.php?bar=123 to newurl/foo/123 and will ignore bar.
Edit: Thinking about it, rewriting your regexp for you won't help you in the long term, and no one except you is likely to know exactly what you want. I think a better course of action is pointing you to a regexp guide. Here is one, and it's specifically targeted for mod_rewrite.
I finally found the solution. I did some more research and I used a different approach which I believe was even better then what i was using
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^([A-Za-z0-9]+)\.php$ /cmstut/redirect/newurl/account/%1? [R=302,L]